自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET Excel動(dòng)態(tài)實(shí)現(xiàn)淺析

開發(fā) 后端
ASP.NET中Excel動(dòng)態(tài)實(shí)現(xiàn)淺析向你介紹如何在ASP.NET中Excel動(dòng)態(tài)實(shí)現(xiàn),這是一篇譯文,希望對(duì)你有所幫助。

ASP.NET Excel動(dòng)態(tài)實(shí)現(xiàn)首先在Asp.net中建立本地的Excel表,并由服務(wù)器向外傳播是容易實(shí)現(xiàn)的,而刪除掉嵌入的Excel.exe進(jìn)程是困難的。所以 你不要打開任務(wù)管理器 ,看Excel.exe進(jìn)程相關(guān)的東西是否還在內(nèi)存里面。我在這里提供一個(gè)解決方案 ,里面提供了兩個(gè)方法 :
 
"CreateExcelWorkbook"(說明 建立ASP.NET Excel動(dòng)態(tài)工作簿) 這個(gè)方法 運(yùn)行一個(gè)存儲(chǔ)過程 ,返回一個(gè)DataReader 并根據(jù)DataReader 來生成一個(gè)Excel工作簿 ,并保存到文件系統(tǒng)中,創(chuàng)建一個(gè)“download”連接,這樣 用戶就可以將Excel表導(dǎo)入到瀏覽器中也可以直接下載到機(jī)器上。

第二個(gè)方法:GenerateCSVReport 本質(zhì)上是做同樣的一件事情,僅僅是保存的文件的CSV格式 。仍然 導(dǎo)入到Excel中,CSV代碼能解決一個(gè)開發(fā)中的普片的問題:你有一列 里面倒入了多個(gè)零,CSV代碼能保證零不變空 。(說明: 就是在Excel表中多個(gè)零的值 不能保存的問題)

在可以下載的解決方案中,包含一個(gè)有效的類 ” SPGen” 能運(yùn)行存儲(chǔ)過程并返回DataReader ,一個(gè)移除文件的方法 能刪除早先于一個(gè)特定的時(shí)間值。下面出現(xiàn)的主要的方法就是CreateExcelWorkbook

注意:你必須知道 在運(yùn)行這個(gè)頁面的時(shí)候,你可能需要能在WebSever 服務(wù)器的文件系統(tǒng)中寫 Excel,Csv文件的管理員的權(quán)限。處理這個(gè)問題的最簡(jiǎn)單的方法就是運(yùn)行這個(gè)頁面在自己的文件夾里面并包括自己的配置文件。并在配置文件中添加下面的元素﹤identity impersonate ="true" ... 。你仍然需要物理文件夾的訪問控制列表(ACL)的寫的權(quán)限,只有這樣運(yùn)行的頁面的身份有寫的權(quán)限,***,你需要設(shè)置一個(gè)Com連接到Excel 9.0 or Excel 10 類型庫(kù) ,VS.NET 將為你生成一個(gè)裝配件。我相信 微軟在他們Office網(wǎng)站上有一個(gè)連接,可以下載到微軟的初始的裝配件 。(可能不準(zhǔn),我的理解是面向.net的裝配件)

  1. ﹤identity impersonate="true" userName="adminuser" password="adminpass" /﹥  

特別注意 下面的代碼塊的作用是清除ASP.NET Excel動(dòng)態(tài)的對(duì)象。

  1. // Need all following code to clean up and extingush all references!!!  
  2. oWB.Close(null,null,null);  
  3. oXL.Workbooks.Close();  
  4. oXL.Quit();  
  5. System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);  
  6. System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);  
  7. System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);  
  8. System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);  
  9. oSheet=null;  
  10. oWB=null;  
  11. oXL = null;  
  12. GC.Collect(); // force final cleanup! 

這是必須的 ,因?yàn)閛Sheet", "oWb" , 'oRng", 等等 對(duì)象也是COM的實(shí)例,我們需要Marshal類的ReleaseComObject的方法把它們從.NET去掉

  1. private void CreateExcelWorkbook(string spName, SqlParameter[] parms)  
  2. {  
  3. string strCurrentDir = Server.MapPath(".") + "";  
  4. RemoveFiles(strCurrentDir); // utility method to clean up old files   
  5. Excel.Application oXL;  
  6. Excel._Workbook oWB;  
  7. Excel._Worksheet oSheet;  
  8. Excel.Range oRng;   
  9.  
  10. try 
  11. {  
  12. GC.Collect();// clean up any other excel guys hangin' around...  
  13. oXL = new Excel.Application();  
  14. oXL.Visible = false;  
  15. //Get a new workbook.  
  16. oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));  
  17. oSheet = (Excel._Worksheet)oWB.ActiveSheet;  
  18. //get our Data   
  19.  
  20. string strConnect = System.Configuration.ConfigurationSettings.AppSettings["connectString"];  
  21. SPGen sg = new SPGen(strConnect,spName,parms);   
  22. SqlDataReader myReader = sg.RunReader();   
  23. // Create Header and sheet...  
  24. int iRow =2;   
  25. for(int j=0;j﹤myReader.FieldCount;j++)  
  26. {  
  27. oSheet.Cells[1, j+1] = myReader.GetName(j).ToString();   
  28. }  
  29. // build the sheet contents  
  30. while (myReader.Read())  
  31. {   
  32. for(int k=0;k ﹤ myReader.FieldCount;k++)  
  33. {  
  34. oSheet.Cells[iRow,k+1]= myReader.GetValue(k).ToString();  
  35. }  
  36. iRow++;  
  37. }// end while  
  38. myReader.Close();  
  39. myReader=null;  
  40. //Format A1:Z1 as bold, vertical alignment = center.  
  41. oSheet.get_Range("A1""Z1").Font.Bold = true;  
  42. oSheet.get_Range("A1""Z1").VerticalAlignment =Excel.XlVAlign.xlVAlignCenter;  
  43. //AutoFit columns A:Z.  
  44. oRng = oSheet.get_Range("A1""Z1");  
  45. oRng.EntireColumn.AutoFit();  
  46. oXL.Visible = false;  
  47. oXL.UserControl = false;  
  48. string strFile ="report" + System.DateTime.Now.Ticks.ToString() +".xls";  
  49. oWB.SaveAs( strCurrentDir + strFile,Excel.XlFileFormat.xlWorkbookNormal,  
  50.      null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);  
  51. // Need all following code to clean up and extingush all references!!!  
  52. oWB.Close(null,null,null);  
  53. oXL.Workbooks.Close();  
  54. oXL.Quit();  
  55. System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);  
  56. System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);  
  57. System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);  
  58. System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);  
  59. oSheet=null;  
  60. oWB=null;  
  61. oXL = null;  
  62. GC.Collect(); // force final cleanup!  
  63. string strMachineName = Request.ServerVariables["SERVER_NAME"];  
  64. errLabel.Text="﹤A href=http://" + strMachineName +"/ExcelGen/" +strFile + "﹥Download Report﹤/a﹥";   
  65.  
  66. }  
  67. catch( Exception theException )   
  68. {  
  69. String errorMessage;  
  70. errorMessage = "Error: ";  
  71. errorMessage = String.Concat( errorMessage, theException.Message );  
  72. errorMessage = String.Concat( errorMessage, " Line: " );  
  73. errorMessage = String.Concat( errorMessage, theException.Source );   
  74. errLabel.Text= errorMessage ;  
  75. }  

ASP.NET Excel動(dòng)態(tài)實(shí)現(xiàn)的基本情況就向你介紹到這里,希望對(duì)你有所幫助。

【編輯推薦】

  1. ASP.NET中彈出窗口常見的封殺方式淺談
  2. ASP.NET數(shù)據(jù)庫(kù)編程技術(shù)淺析
  3. ASP.NET程序員的學(xué)習(xí)之路雜談
  4. ASP.NET自定義控件開發(fā)淺析
  5. ASP.NET服務(wù)器控件之生命周期淺析
責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-07-28 10:26:30

ASP.NET操作Ex

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點(diǎn)

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-07-24 10:53:51

ASP.NET實(shí)現(xiàn)靜態(tài)

2009-08-04 10:02:36

中國(guó)站長(zhǎng)站

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-08-04 17:16:16

ASP.NET代碼優(yōu)化

2009-08-05 16:50:09

ASP.NET For

2009-07-24 18:02:46

ASP.NET編程

2009-07-27 15:34:11

MembershipASP.NET

2009-07-27 17:25:53

ASP.NET驗(yàn)證控件

2009-08-03 10:07:20

ASP.NET Ses

2009-08-05 13:16:43

ASP.NET URL

2009-08-05 16:17:29

ASP.NET For

2009-08-05 16:53:14

ASP.NET組件設(shè)計(jì)

2009-08-10 18:19:37

ASP.NET安裝環(huán)境

2009-08-10 18:43:05

ASP.NET安裝步驟
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)