1、增加引用:Microsoft.Office.Interop.Excel
2、引用命名空间:using Excel = Microsoft.Office.Interop.Excel;
3、导入主要代码:
DataTable tempTable = new DataTable(); //save to excel // write to excel var App = new Excel.Application(); //Microsoft.Office.Interop.Excel.Workbook Object oMissing = System.Reflection.Missing.Value; Excel.Workbook workBook = App.Workbooks.Add(oMissing); Excel.Worksheet sheet = (Excel.Worksheet)workBook.Sheets[1]; //headline sheet.Cells[1, 1] = "source"; sheet.Cells[1, 2] = "translation"; //int row = 2; for (int i = 0; i < tempTable.Rows.Count; i++) { for (int j = 0; j < tempTable.Columns.Count; j++) { sheet.Cells[i + 2, j + 1] = tempTable.Rows[i][j].ToString(); } } //workBook.SaveAs(@"E:\chao\Practise\xml\test.xlsx", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); sheet.SaveAs(@"C:\Temp\ESM.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing); workBook.Close(false, Type.Missing, Type.Missing);