Web DataGrid 导出Execl,速度很慢怎么办?
private void DataTableToExecl(System.Data.DataTable dt)
{
Excel.Application oExcel=new Excel.Application();
Excel.Workbooks oBooks;
Excel.Workbook oBook;
Excel.Sheets oSheets;
Excel.Worksheet oSheet;
Excel.Range oCells;
string sFile,sTemplate;
sFile=Server.MapPath(Request.ApplicationPath) + const_ExeclPrintFileName;
sTemplate=Server.MapPath(Request.ApplicationPath) + const_ExeclTemplateFileName ;
oExcel.DisplayAlerts=false;
oBooks=oExcel.Workbooks;
try
{
oBooks.Open(Server.MapPath(Request.ApplicationPath) + const_ExeclTemplateFileName ,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value) ;
}
catch (Exception ez)
{
throw ez;
}
oBook=oBooks[1];
oSheets=oBook.Worksheets;
oSheet=(Excel.Worksheet) oSheets[1];
oSheet.Name="First Sheet";
oCells=oSheet.Cells;
DumpData(dt,oCells);
oSheet.SaveAs(sFile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value) ;
oBook.Close(Missing.Value,Missing.Value,Missing.Value);
oExcel.Quit();
Marshal.ReleaseComObject(oCells);
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oSheets) ;
Marshal.ReleaseComObject(oBook);
Marshal.ReleaseComObject(oBooks);
Marshal.ReleaseComObject(oExcel);
oExcel = null;
oBooks = null;
oBook = null;
oSheets = null;
oSheet = null ;
oCells = null;
System.GC.Collect();
}
private void DumpData(System.Data.DataTable dt, Excel.Range oCells)
{
int iCol;
int iRow;
DataRow dr;
object[] ary;
for (iCol=0;iCol<dt.Columns.Count;iCol++)
{
oCells[2,iCol+1]=dt.Columns[iCol].ToString();
}
for (iRow=0 ;iRow<dt.Rows.Count;iRow++)
{
dr=dt.Rows[iRow];
ary=dr.ItemArray;
for(iCol=0 ;iCol<=ary.GetUpperBound(0);iCol++)
{
oCells[iRow+3,iCol+1]=ary[iCol].ToString();
}
}
}