private void AddDataToExcel(System.Data.DataTable dt, string filename)
{
try
{
//write
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
//excelApplication.Visible = true;
#region//填充数据
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
excelApplication.Cells[i + 1, j + 1] = dt.Rows[i][j];
//Value.ToString();
}
}
#endregion
//workbook.SaveCopyAs (filepathoutput +"\\"+ strB + ".xlsx");
worksheet.SaveAs(filename + ".xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
worksheet = null;
workbook.Close(false, Missing.Value, Missing.Value);
excelApplication.Quit();
excelApplication = null;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}