(相关资料图)

[ private void SaveTo(System.Data.DataTable table, string fileName) { int columnIndex = 1; int rowIndex = 1; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.DefaultFilePath = fileName; excelApp.DisplayAlerts = true; excelApp.SheetsInNewWorkbook = 1; Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(true); foreach (System.Data.DataColumn column in table.Columns) { excelApp.Cells[rowIndex, columnIndex] = column.ColumnName; columnIndex++; } for (int i = 0; i < table.Rows.Count; i++) { columnIndex = 1; rowIndex++; for (int j = 0; j < table.Columns.Count; j++) { excelApp.Cells[rowIndex, columnIndex] = table.Rows[i][j].ToString(); columnIndex++; } } workbook.SaveCopyAs(fileName); excelApp = null; workbook = null; }

推荐内容