您的位置:首页 > 编程语言 > ASP

Asp.net 导出Excel 处理科学计算法

2012-03-02 16:11 459 查看
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐