ASP.NET中怎么调用Excel进程

发布时间:2021-06-24 16:25:16 作者:Leah
阅读:168
开发者专用服务器限时活动,0元免费领! 查看>>

ASP.NET中怎么调用Excel进程,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

ASP.NET调用Excel进程

关于在ASP.NET调用Excel进程不能结束进程的问题,常见的解决方法用的是下面这段代码

wb.Close(null,null,null);  app.Workbooks.Close();  app.Quit();   if(rng!=null)  {  System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);  rng=null;  }  if(ws!=null)  {  System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);  ws=null;  }  if(wb!=null)  {System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);  wb=null;  }  if(app!=null)  {System.Runtime.InteropServices.Marshal.ReleaseComObject(app);  app=null;  }  GC.Collect();

虽然这段代码在配置正确的情况下能自动结束Excel进程,但是前提是在操作Excel时没有引发异常的情况下,如果有异常发生,那么Excel进程将不能结束(比如:引用了一个在Excel文件中不存在的文本框时就会出现“HRESULT 中的异常:0x800A03EC。”),这时就要借助Process类的Kill()方法来结束,下面是我写的测试代码:

  1. usingSystem;  

  2. usingSystem.Diagnostics;  

  3. usingexcel=Microsoft.Office.Interop.Excel;  

  4. namespaceExcelTest  

  5. {  

  6. /**////<summary> 

  7. ///Excel的摘要说明。  

  8. ///</summary> 

  9. publicclassExcel  

  10. {  

  11. privateDateTimebeforeTime;//Excel启动之前时间  

  12. privateDateTimeafterTime;//Excel启动之后时间  

  13. excel.Applicationapp;  

  14. excel.Workbookwb;  

  15. excel.Worksheetws;  

  16. excel.Rangerng;  

  17. excel.TextBoxtb;  

  18. publicExcel(stringtempletPath)  

  19. {  

  20. //实例化一个ExcelApplication对象并使其可见  

  21. beforeTime=DateTime.Now;  

  22. app=newexcel.ApplicationClass();  

  23. app.Visible=true;  

  24. afterTime=DateTime.Now;  

  25. wb=app.Workbooks.Open(templetPath,Type.Missing,Type.Missing,Type.
    Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.
    Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.
    Missing,Type.Missing);  

  26. ws=(excel.Worksheet)wb.Worksheets.get_Item(1);  

  27. }  

  28. publicvoidExcelMethod()  

  29. {  

  30. rng=ws.get_Range("B5","C7");  

  31. rng.Merge(excel.XlAxisCrosses.xlAxisCrossesAutomatic);  

  32. rng.Value2="Excel2003";  

  33. rng=ws.get_Range("D8","E11");  

  34. rng.MergeCells=true;  

  35. rng.Value2="Excel2003";  

  36. rng.HorizontalAlignment=excel.XlHAlign.xlHAlignCenter;  

  37. rng.VerticalAlignment=excel.XlVAlign.xlVAlignCenter;  

  38. rng=ws.get_Range("A1",Type.Missing);  

  39. rng.Value2=5;  

  40. rng=ws.get_Range("A2",Type.Missing);  

  41. rng.Value2=7;  

  42. for(inti=1;i<100;i++)  

  43. {  

  44. stringstrings=string.Concat("G",i.ToString());  

  45. rng=ws.get_Range(s,Type.Missing);  

  46. rng.Value2=i.ToString();  

  47. }  

  48. tb=(excel.TextBox)ws.TextBoxes("文本框1");  

  49. tb.Text="作者";  

  50. tb=(excel.TextBox)ws.TextBoxes("文本框2");  

  51. tb.Text="KLY.NET的Blog";  

  52. tb=(excel.TextBox)ws.TextBoxes("文本框3");  

  53. tb.Text="日期";  

  54. try  

  55. {  

  56. tb=(excel.TextBox)ws.TextBoxes("文本框5");  

  57. tb.Text=DateTime.Now.ToShortDateString();  

  58. }  

  59. catch  

  60. {  

  61. //这里用Dispose()方法结束不了Excel进程,所有还是要用Process的Kill()方法配合使用  

  62. //this.Dispose();  

  63. this.KillExcelProcess();  

  64. thrownewException("不存在ID为\"文本框5\"的文本框!");  

  65. }  

  66. finally  

  67. {  

  68. //如果有异常发生,Dispose()方法放在这里也结束不了Excel进程  

  69. //this.Dispose();  

  70. //如果发生异常,在这里也可以结束Excel进程  

  71. //this.KillExcelProcess();  

  72. }  

  73. }  

  74. /**////<summary> 

  75. ///另存为Excel文件  

  76. ///</summary> 

  77. ///<paramnameparamname="savePath">保存路径</param> 

  78. publicvoidSaveAsExcelFile(stringsavePath)  

  79. {  

  80. wb.SaveAs(savePath,excel.XlFileFormat.xlHtml,Type.Missing,Type.
    Missing,Type.Missing,Type.Missing,excel.XlSaveAsAccessMode.xlExclusive,Type.
    Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);  

  81. }  

  82. /**////<summary> 

  83. ///结束Excel进程  

  84. ///</summary> 

  85. publicvoidKillExcelProcess()  

  86. {  

  87. Process[]myProcesses;  

  88. DateTimestartTime;  

  89. myProcesses=Process.GetProcessesByName("Excel");  

  90. //得不到Excel进程ID,暂时只能判断进程启动时间  

  91. foreach(ProcessmyProcessinmyProcesses)  

  92. {  

  93. startTime=myProcess.StartTime;  

  94. if(startTime>beforeTime&&startTime<afterTime)  

  95. {  

  96. myProcess.Kill();  

  97. }  

  98. }  

  99. }  

  100. /**////<summary> 

  101. ///如果对Excel的操作没有引发异常的话,用这个方法可以正常结束Excel进程  

  102. ///否则要用KillExcelProcess()方法来结束Excel进程  

  103. ///</summary> 

  104. publicvoidDispose()  

  105. {  

  106. wb.Close(null,null,null);  

  107. app.Workbooks.Close();  

  108. app.Quit();  

  109. //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程  

  110. if(rng!=null)  

  111. {  

  112. System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);  

  113. rng=null;  

  114. }  

  115. if(tb!=null)  

  116. {  

  117. System.Runtime.InteropServices.Marshal.ReleaseComObject(tb);  

  118. tb=null;  

  119. }  

  120. if(ws!=null)  

  121. {  

  122. System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);  

  123. ws=null;  

  124. }  

  125. if(wb!=null)  

  126. {  

  127. System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);  

  128. wb=null;  

  129. }  

  130. if(app!=null)  

  131. {  

  132. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);  

  133. app=null;  

  134. }  

  135. GC.Collect();  

  136. }  

  137. }  

这段代码能很好的解决Excel进程不能正常结束的问题,如果主机操作系统不是服务器版的话,那么就要借助于ntsd -c q -p pid命令来结束。

还有一个问题的关于Excel组件访问权限的配置,一定要在组件服务里面正确配置,否则结束不了Excel进程,具体的配置方法在我项目的doc文件夹下;在我前面的文章里面介绍了在web.config文件里面加入假扮用户的方法,但是经我测试发现这种方法虽然可以访问Excel组件,但是结束不了进程,除非用Kill方法强行结束。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. 如何使用C#结束Excel进程
  2. 怎么在Asp.Net中利用oleDbConnection 连接Excel

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:C#中怎么调用外部进程

下一篇:C#中怎么调用SQL存储过程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×