參考來源
2012年1月31日 星期二
rdlc匯出excel或PDF
參考引用
--
private DataTable LoadSalesData()
{
// Load data from XML file.
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"c:\Reports\data.xml");
return dataSet.Tables[0];
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = @"c:\Reports\Report1.rdl";
reportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("Sales", LoadSalesData()));
this.reportViewer1.RefreshReport();
}
private void button1_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = reportViewer1.LocalReport.Render(
"Excel", null, out mimeType, out encoding, out extension,
out streamids, out warnings);
FileStream fs = new FileStream(@"c:\output.xls", FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
MessageBox.Show("Report exported to output.xls", "Info");
}
--
private DataTable LoadSalesData()
{
// Load data from XML file.
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"c:\Reports\data.xml");
return dataSet.Tables[0];
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = @"c:\Reports\Report1.rdl";
reportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("Sales", LoadSalesData()));
this.reportViewer1.RefreshReport();
}
private void button1_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = reportViewer1.LocalReport.Render(
"Excel", null, out mimeType, out encoding, out extension,
out streamids, out warnings);
FileStream fs = new FileStream(@"c:\output.xls", FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
MessageBox.Show("Report exported to output.xls", "Info");
}
Excel Data Reader - Read Excel files in .NET
Excel Data Reader - Read Excel files in .NET 元件
--
不想寫code的,可使用上面這元件來讀 excel 檔
引用官網 code:
c#:
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
vbnet:
Dim stream As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read)
'1. Reading from a binary Excel file ('97-2003 format; *.xls)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
'...
'2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
'...
'3. DataSet - The result of each spreadsheet will be created in the result.Tables
Dim result As DataSet = excelReader.AsDataSet()
'...
'4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = True
Dim result As DataSet = excelReader.AsDataSet()
'5. Data Reader methods
While excelReader.Read()
'excelReader.GetInt32(0);
End While
'6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close()
--
不想寫code的,可使用上面這元件來讀 excel 檔
引用官網 code:
c#:
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
vbnet:
Dim stream As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read)
'1. Reading from a binary Excel file ('97-2003 format; *.xls)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
'...
'2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
'...
'3. DataSet - The result of each spreadsheet will be created in the result.Tables
Dim result As DataSet = excelReader.AsDataSet()
'...
'4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = True
Dim result As DataSet = excelReader.AsDataSet()
'5. Data Reader methods
While excelReader.Read()
'excelReader.GetInt32(0);
End While
'6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close()
vbnet 讀取 excel 採用 OleDbDataReader
參考引用來源
--
Using m_connexcel As OleDbConnection = New OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source={0};" & _
"Extended Properties=""Excel 8.0;HDR=NO""", SPREADSHEET_NAME))
m_connexcel.Open()
Using cmd As OleDbCommand = New OleDbCommand(String.Format("SELECT * FROM [{0}]", DATARANGE), m_connexcel)
Using oRDR As OleDbDataReader = cmd.ExecuteReader
While (oRDR.Read)
If Not oRDR.IsDBNull(oRDR.GetOrdinal("F1")) Then
MsgBox(oRDR.GetString(oRDR.GetOrdinal("F1")))
End If
End While
End Using
End Using
m_connexcel.Close()
End Using
說明:
Set 'HDR' in the connection string to 'YES' if you want to use the first row as the field names, otherwise the fields will be called
F1, F2, F3 etc...
--
Using m_connexcel As OleDbConnection = New OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source={0};" & _
"Extended Properties=""Excel 8.0;HDR=NO""", SPREADSHEET_NAME))
m_connexcel.Open()
Using cmd As OleDbCommand = New OleDbCommand(String.Format("SELECT * FROM [{0}]", DATARANGE), m_connexcel)
Using oRDR As OleDbDataReader = cmd.ExecuteReader
While (oRDR.Read)
If Not oRDR.IsDBNull(oRDR.GetOrdinal("F1")) Then
MsgBox(oRDR.GetString(oRDR.GetOrdinal("F1")))
End If
End While
End Using
End Using
m_connexcel.Close()
End Using
說明:
Set 'HDR' in the connection string to 'YES' if you want to use the first row as the field names, otherwise the fields will be called
F1, F2, F3 etc...
vbnet 讀取 excel 採用 OleDb dataset
參考引用來源
--
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim myPath As String = "D:\CourseFootprints.xls"
Try
Dim DS As System.Data.DataSet
Dim strPath, strSheet As String
strPath = "D:\Test.xls"
strSheet = "Sheet1"
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & strPath & ";Extended Properties=""Excel 8.0;IMEX=1;HDR=YES;""")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [" & strSheet & "$]", MyConnection)
DS = New System.Data.DataSet
MyCommand.Fill(DS)
Dim dv As DataView
dv = DS.Tables(0).DefaultView
dgGridVw.DataSource = dv
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
訂閱:
文章 (Atom)
