Asp.net 导出Excel 处理科学计算法
来源:互联网 发布:java代码执行顺序 编辑:程序博客网 时间:2024/06/06 02:49
public static void doExport(DataSet ds, string strExcelFileName) { Excel.Application excel = new Excel.Application(); int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true); System.Data.DataTable table = ds.Tables[0]; foreach(DataColumn col in table.Columns) { colIndex++; excel.Cells[1, colIndex] = col.ColumnName; } foreach(DataRow row in table.Rows) { rowIndex++; colIndex = 0; Excel.Range range; foreach(DataColumn col in table.Columns) { colIndex++; string type = col.DataType.ToString(); range = (Excel.Range) excel.Cells[rowIndex, colIndex]; switch (type) { case "System.String": range.NumberFormatLocal = "@"; break; case "System.DateTime": range.NumberFormatLocal = "yyyy-mm-dd"; range.ColumnWidth = 10; break; } excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } excel.Visible = false; excel.ActiveWorkbook.SaveAs (strExcelFileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAcce ssMode.xlNoChange, null, null, null, null); excel.Quit(); excel = null; GC.Collect();}