ASP.NET Excel動態(tài)實現(xiàn)淺析
ASP.NET Excel動態(tài)實現(xiàn)首先在Asp.net中建立本地的Excel表,并由服務(wù)器向外傳播是容易實現(xiàn)的,而刪除掉嵌入的Excel.exe進(jìn)程是困難的。所以 你不要打開任務(wù)管理器 ,看Excel.exe進(jìn)程相關(guān)的東西是否還在內(nèi)存里面。我在這里提供一個解決方案 ,里面提供了兩個方法 :
 
"CreateExcelWorkbook"(說明 建立ASP.NET Excel動態(tài)工作簿) 這個方法 運行一個存儲過程 ,返回一個DataReader 并根據(jù)DataReader 來生成一個Excel工作簿 ,并保存到文件系統(tǒng)中,創(chuàng)建一個“download”連接,這樣 用戶就可以將Excel表導(dǎo)入到瀏覽器中也可以直接下載到機(jī)器上。
第二個方法:GenerateCSVReport 本質(zhì)上是做同樣的一件事情,僅僅是保存的文件的CSV格式 。仍然 導(dǎo)入到Excel中,CSV代碼能解決一個開發(fā)中的普片的問題:你有一列 里面倒入了多個零,CSV代碼能保證零不變空 。(說明: 就是在Excel表中多個零的值 不能保存的問題)
在可以下載的解決方案中,包含一個有效的類 ” SPGen” 能運行存儲過程并返回DataReader ,一個移除文件的方法 能刪除早先于一個特定的時間值。下面出現(xiàn)的主要的方法就是CreateExcelWorkbook
注意:你必須知道 在運行這個頁面的時候,你可能需要能在WebSever 服務(wù)器的文件系統(tǒng)中寫 Excel,Csv文件的管理員的權(quán)限。處理這個問題的最簡單的方法就是運行這個頁面在自己的文件夾里面并包括自己的配置文件。并在配置文件中添加下面的元素﹤identity impersonate ="true" ... 。你仍然需要物理文件夾的訪問控制列表(ACL)的寫的權(quán)限,只有這樣運行的頁面的身份有寫的權(quán)限,***,你需要設(shè)置一個Com連接到Excel 9.0 or Excel 10 類型庫 ,VS.NET 將為你生成一個裝配件。我相信 微軟在他們Office網(wǎng)站上有一個連接,可以下載到微軟的初始的裝配件 。(可能不準(zhǔn),我的理解是面向.net的裝配件)
- ﹤identity impersonate="true" userName="adminuser" password="adminpass" /﹥
 
特別注意 下面的代碼塊的作用是清除ASP.NET Excel動態(tài)的對象。
- // Need all following code to clean up and extingush all references!!!
 - oWB.Close(null,null,null);
 - oXL.Workbooks.Close();
 - oXL.Quit();
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
 - oSheet=null;
 - oWB=null;
 - oXL = null;
 - GC.Collect(); // force final cleanup!
 
這是必須的 ,因為oSheet", "oWb" , 'oRng", 等等 對象也是COM的實例,我們需要Marshal類的ReleaseComObject的方法把它們從.NET去掉
- private void CreateExcelWorkbook(string spName, SqlParameter[] parms)
 - {
 - string strCurrentDir = Server.MapPath(".") + "";
 - RemoveFiles(strCurrentDir); // utility method to clean up old files
 - Excel.Application oXL;
 - Excel._Workbook oWB;
 - Excel._Worksheet oSheet;
 - Excel.Range oRng;
 - try
 - {
 - GC.Collect();// clean up any other excel guys hangin' around...
 - oXL = new Excel.Application();
 - oXL.Visible = false;
 - //Get a new workbook.
 - oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
 - oSheet = (Excel._Worksheet)oWB.ActiveSheet;
 - //get our Data
 - string strConnect = System.Configuration.ConfigurationSettings.AppSettings["connectString"];
 - SPGen sg = new SPGen(strConnect,spName,parms);
 - SqlDataReader myReader = sg.RunReader();
 - // Create Header and sheet...
 - int iRow =2;
 - for(int j=0;j﹤myReader.FieldCount;j++)
 - {
 - oSheet.Cells[1, j+1] = myReader.GetName(j).ToString();
 - }
 - // build the sheet contents
 - while (myReader.Read())
 - {
 - for(int k=0;k ﹤ myReader.FieldCount;k++)
 - {
 - oSheet.Cells[iRow,k+1]= myReader.GetValue(k).ToString();
 - }
 - iRow++;
 - }// end while
 - myReader.Close();
 - myReader=null;
 - //Format A1:Z1 as bold, vertical alignment = center.
 - oSheet.get_Range("A1", "Z1").Font.Bold = true;
 - oSheet.get_Range("A1", "Z1").VerticalAlignment =Excel.XlVAlign.xlVAlignCenter;
 - //AutoFit columns A:Z.
 - oRng = oSheet.get_Range("A1", "Z1");
 - oRng.EntireColumn.AutoFit();
 - oXL.Visible = false;
 - oXL.UserControl = false;
 - string strFile ="report" + System.DateTime.Now.Ticks.ToString() +".xls";
 - oWB.SaveAs( strCurrentDir + strFile,Excel.XlFileFormat.xlWorkbookNormal,
 - null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);
 - // Need all following code to clean up and extingush all references!!!
 - oWB.Close(null,null,null);
 - oXL.Workbooks.Close();
 - oXL.Quit();
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
 - System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
 - oSheet=null;
 - oWB=null;
 - oXL = null;
 - GC.Collect(); // force final cleanup!
 - string strMachineName = Request.ServerVariables["SERVER_NAME"];
 - errLabel.Text="﹤A href=http://" + strMachineName +"/ExcelGen/" +strFile + "﹥Download Report﹤/a﹥";
 - }
 - catch( Exception theException )
 - {
 - String errorMessage;
 - errorMessage = "Error: ";
 - errorMessage = String.Concat( errorMessage, theException.Message );
 - errorMessage = String.Concat( errorMessage, " Line: " );
 - errorMessage = String.Concat( errorMessage, theException.Source );
 - errLabel.Text= errorMessage ;
 - }
 - }
 
ASP.NET Excel動態(tài)實現(xiàn)的基本情況就向你介紹到這里,希望對你有所幫助。
【編輯推薦】















 
 
 
 
 
 
 