private void ExportExcel(System.Data.DataTable table,string path,string[] captions)
{
Excel.Application excel = new Excel.ApplicationClass();
Excel._Workbook xBk = excel.Workbooks.Add(true);
Excel._Worksheet xSt = (Excel._Worksheet)xBk.ActiveSheet;
//Excel.WorksheetClass worksheet= Excel.WorksheetClass)excel.ActiveSheet;for(int i = 0;i < captions.Length;i ++) excel.Cells[1,i+1] = captions[i];for(int i = 0;i < table.Rows.Count;i ++)
{
for(int j = 0;j < table.Columns.Count;j ++)
{
excel.Cells[i+2,j+1] = table.Rows[i][j];
}
}xSt.get_Range(excel.Cells[1,1],excel.Cells[table.Rows.Count + 1,captions.Length]).Columns.AutoFit();excel.Visible = false;
excel.DisplayAlerts = false;
excel.Save(path);
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
{
Excel.Application excel = new Excel.ApplicationClass();
Excel._Workbook xBk = excel.Workbooks.Add(true);
Excel._Worksheet xSt = (Excel._Worksheet)xBk.ActiveSheet;
//Excel.WorksheetClass worksheet= Excel.WorksheetClass)excel.ActiveSheet;for(int i = 0;i < captions.Length;i ++) excel.Cells[1,i+1] = captions[i];for(int i = 0;i < table.Rows.Count;i ++)
{
for(int j = 0;j < table.Columns.Count;j ++)
{
excel.Cells[i+2,j+1] = table.Rows[i][j];
}
}xSt.get_Range(excel.Cells[1,1],excel.Cells[table.Rows.Count + 1,captions.Length]).Columns.AutoFit();excel.Visible = false;
excel.DisplayAlerts = false;
excel.Save(path);
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
xBook.SaveAsexcelSavePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);