ASP.NET Excel动态实现浅析

开发 后端
ASP.NET中Excel动态实现浅析向你介绍如何在ASP.NET中Excel动态实现,这是一篇译文,希望对你有所帮助。

ASP.NET Excel动态实现首先在Asp.net中建立本地的Excel表,并由服务器向外传播是容易实现的,而删除掉嵌入的Excel.exe进程是困难的。所以 你不要打开任务管理器 ,看Excel.exe进程相关的东西是否还在内存里面。我在这里提供一个解决方案 ,里面提供了两个方法 :
 
"CreateExcelWorkbook"(说明 建立ASP.NET Excel动态工作簿) 这个方法 运行一个存储过程 ,返回一个DataReader 并根据DataReader 来生成一个Excel工作簿 ,并保存到文件系统中,创建一个“download”连接,这样 用户就可以将Excel表导入到浏览器中也可以直接下载到机器上。

第二个方法:GenerateCSVReport 本质上是做同样的一件事情,仅仅是保存的文件的CSV格式 。仍然 导入到Excel中,CSV代码能解决一个开发中的普片的问题:你有一列 里面倒入了多个零,CSV代码能保证零不变空 。(说明: 就是在Excel表中多个零的值 不能保存的问题)

在可以下载的解决方案中,包含一个有效的类 ” SPGen” 能运行存储过程并返回DataReader ,一个移除文件的方法 能删除早先于一个特定的时间值。下面出现的主要的方法就是CreateExcelWorkbook

注意:你必须知道 在运行这个页面的时候,你可能需要能在WebSever 服务器的文件系统中写 Excel,Csv文件的管理员的权限。处理这个问题的最简单的方法就是运行这个页面在自己的文件夹里面并包括自己的配置文件。并在配置文件中添加下面的元素﹤identity impersonate ="true" ... 。你仍然需要物理文件夹的访问控制列表(ACL)的写的权限,只有这样运行的页面的身份有写的权限,***,你需要设置一个Com连接到Excel 9.0 or Excel 10 类型库 ,VS.NET 将为你生成一个装配件。我相信 微软在他们Office网站上有一个连接,可以下载到微软的初始的装配件 。(可能不准,我的理解是面向.net的装配件)

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

特别注意 下面的代码块的作用是清除ASP.NET Excel动态的对象。

  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! 

这是必须的 ,因为oSheet", "oWb" , 'oRng", 等等 对象也是COM的实例,我们需要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动态实现的基本情况就向你介绍到这里,希望对你有所帮助。

【编辑推荐】

  1. ASP.NET中弹出窗口常见的封杀方式浅谈
  2. ASP.NET数据库编程技术浅析
  3. ASP.NET程序员的学习之路杂谈
  4. ASP.NET自定义控件开发浅析
  5. ASP.NET服务器控件之生命周期浅析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-07-28 10:26:30

ASP.NET操作Ex

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET优点

2009-08-04 10:02:36

中国站长站

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-08-05 16:59:55

ASP.NET组件设计

2009-07-24 10:53:51

ASP.NET实现静态

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-28 16:40:11

ASP.NET异步页面

2009-08-05 14:46:17

ASP.NET url

2009-07-23 14:31:20

ASP.NET MVC

2009-08-04 17:00:09

ASP.NET禁用Vi

2009-07-20 16:23:01

ASP.NET授权模块

2009-07-28 15:53:43

ASP.NET Web

2009-07-27 10:18:12

TypeResolveASP.NET

2009-07-28 13:35:18

2009-08-04 14:18:49

ASP.NET邮件列表

2009-07-24 18:02:46

ASP.NET编程

2009-08-04 17:16:16

ASP.NET代码优化
点赞
收藏

51CTO技术栈公众号