ASP.NET DATASET 导出到excel速度很慢
现在要把数据从dataset导出到excel 测试数据1W条,如果只导出小部分的话 速度还能接受,但是全部导出的话,
十几分钟倒不出来。网上虽然有导出的比较快的方法,但是导出的是伪excle文件,不能导入的。下面是我用的代码,也是网上找的:
string excelName ="\\"+ DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls"; string path = Server.MapPath("upfiles") + excelName; Excel.Application excel = new Excel.Application(); //Execl的操作类 //读取保存目标的对象 Excel.Workbook bookDest = (Excel.WorkbookClass)excel.Workbooks.Add(Missing.Value); Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;//给工作薄添加一个Sheet sheetDest.Rows.RowHeight = 20; sheetDest.Name = strSheetName; int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true); foreach (DataColumn col in dt.Columns) { colIndex++; sheetDest.Cells[1, colIndex] = col.ColumnName; } //导入数据行 foreach (DataRow row in dt.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in dt.Columns) { colIndex++; sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } bookDest.Saved = true; bookDest.SaveCopyAs(path); excel.Quit(); excel = null; GC.Collect();