private void Datatabletoexcel (System.Data.DataTable tmpdatatable, String strFileName)
{
if (tmpdatatable = = null)
{
Return
}
int rowNum = tmpdatatable.rows.count;//Line
int columnnum = tmpdatatable.columns.count;//column
int rowIndex = 1;//Line
int columnindex = 0;//column
Excel.Application xlapp = new Excel.Application ();//
/*application Object-related properties
ThisWorkbook Properties */
Xlapp.defaultfilepath = "";//This property returns or sets the default path that is used when the application saves the file. A String type that can be read and written.
Xlapp.displayalerts = true;//Display warning message
Xlapp.sheetsinnewworkbook = 1;//Returns or sets the number of worksheets that Microsoft Excel automatically inserts into a new workbook. A Long type, readable and writable.
Excel.Workbook xlbook = XlApp.Workbooks.Add (TRUE);//Represents a Microsoft.Office.Interop.Excel.Workbook object that exposes an event and serves as a component container .
|
oexcel. APPLICATION.WORKBOOKS.ADD (TRUE); This code means creating a worksheet |
|
Import the column name of a DataTable into the first row of an Excel table
foreach (DataColumn dc in Tmpdatatable.columns)
{
columnindex++;
Xlapp.cells[rowindex, ColumnIndex] = DC. ColumnName;
}
//Import data from a DataTable into Excel
for (int i = 0; i < rowNum; i++)
{
rowindex++;
columnindex = 0;
for (i NT J = 0; J < Columnnum; J + +)
{
columnindex++;
Xlapp.cells[rowindex, columnindex] = tmpdatatable.rows[i][j]. ToString ();
}
}
Xlbook.savecopyas (strFileName + ". xls");
}
}
}