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();
}
Asp.net 导出Excel 处理科学计算法
最新推荐文章于 2015-04-03 13:12:41 发布