我用這個belove代碼得到了準確的結果.....
這可能會幫助你..
try
{
if (dt == null || dt.Columns.Count == 0)
{
throw new Exception("ExportToExcel: Null or empty input table!\n");
}
string Filepath = "E:Sample\\Form.xlsx";
string SheetName = "Elnath - 3000";
// Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel._Worksheet xlWorkSheet;
// Microsoft.Office.Interop.Excel.Range xlRange = null;
object misValue = Type.Missing;
Excel.Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(Filepath);
// xlWorkBook = xlApp.Workbooks.Open(Filepath, misValue, false, misValue, misValue, misValue, true, misValue, misValue, misValue, misValue, misValue, false, misValue, misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[SheetName];
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet;
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets.get_Item(SheetName);
xlWorkSheet.Activate();
// object StartRange = "B";
// object EndRange = misValue;
// xlRange = xlWorkSheet.get_Range("A", "M");
// xlRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.get_Range(StartRange, misValue);
//Header
//for (i = 0; i < dt.Columns.Count; i++)
//{
// xlRange.Cells[1, i + 1] = dt.Columns[i].ColumnName;
//}
//Datas
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
xlApp.Cells[i + 13, j + 1] = dt.Rows[i][j].ToString();
}
}
if (Filepath != null || Filepath != "")
{
try
{
xlApp.ActiveWorkbook.SaveAs(Filepath);
xlApp.Quit();
xlWorkSheet = null;
xlWorkBook = null;
xlApp = null;
}
catch (Exception ex)
{
throw new Exception("Can not save file" + ex.Message);
}
}
else
{
xlApp.Visible = true;
}
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
儘量按照這個線程。 http://stackoverflow.com/questions/1746701/export-datatable-to-excel-file 問候 –