/// <summary>
/// 直接导出Excel
/// </summary>
/// <param name="ds">数据源DataTable </param>
/// <param name="fileName">保存文件名(例如:E:\a.xls)</param>
/// <returns></returns>
public bool DoExport(DataTable table)
{
Excel.Application excel = new Excel.ApplicationClass();
int rowindex = 1;
int colindex = 0;
Excel.Workbook work = excel.Workbooks.Add(true);
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[1, colindex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowindex++;
colindex = 0;
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString();
}
}
excel.Visible = true;
/// 直接导出Excel
/// </summary>
/// <param name="ds">数据源DataTable </param>
/// <param name="fileName">保存文件名(例如:E:\a.xls)</param>
/// <returns></returns>
public bool DoExport(DataTable table)
{
Excel.Application excel = new Excel.ApplicationClass();
int rowindex = 1;
int colindex = 0;
Excel.Workbook work = excel.Workbooks.Add(true);
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[1, colindex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowindex++;
colindex = 0;
foreach (DataColumn col in table.Columns)
{
colindex++;
excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString();
}
}
excel.Visible = true;
excel.ActiveWorkbook.SaveAs(FileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
// excel.ActiveWorkbook.Close(false, null, false);
excel.DisplayAlerts = false;
work.Close(false, null, false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(work);
work = null;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();
return true;
}
浙公网安备 33010602011771号