Overview ASP. NET Excel Process calling

Source: Internet
Author: User

ASP. NET calls the Excel Process

The common solution to the problem that the Excel process cannot end in ASP. NET calling is the following code.

 
 
  1. wb.Close(null,null,null);  
  2. app.Workbooks.Close();  
  3. app.Quit();  
  4.  
  5. if(rng!=null)  
  6. {  
  7. System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);  
  8. rng=null;  
  9. }  
  10. if(ws!=null)  
  11. {  
  12. System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);  
  13. ws=null;  
  14. }  
  15. if(wb!=null)  
  16. {System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);  
  17. wb=null;  
  18. }  
  19. if(app!=null)  
  20. {System.Runtime.InteropServices.Marshal.ReleaseComObject(app);  
  21. app=null;  
  22. }  
  23. GC.Collect(); 

Although this code can automatically end the Excel process when the configuration is correct, the premise is that when the Excel operation does not cause an exception, if an exception occurs, the Excel process cannot end. For example, when a text box that does not exist in the Excel file is referenced, an exception in "HRESULT: 0x800A03EC. "), The Kill () method of the Process class is used to end the Process. The following is the test code I wrote:

 
 
  1. UsingSystem;
  2. UsingSystem. Diagnostics;
  3. Usingexcel=Microsoft. Office. Interop. Excel;
  4.  
  5. NamespaceExcelTest
  6. {
  7. /**////<Summary> 
  8. /// Summary in Excel.
  9. ///</Summary> 
  10. PublicclassExcel
  11. {
  12. PrivateDateTimebeforeTime; // time before Excel start
  13. PrivateDateTimeafterTime; // time after Excel startup
  14.  
  15. Excel. Applicationapp;
  16. Excel. Workbookwb;
  17. Excel. Worksheetws;
  18. Excel. Rangerng;
  19. Excel. TextBoxtb;
  20.  
  21. PublicExcel (stringtempletPath)
  22. {
  23. // Instantiate an ExcelApplication object and make it visible
  24. BeforeTime=DateTime. Now;
  25. App=Newexcel. ApplicationClass ();
  26. App. Visible=True;
  27. AfterTime=DateTime. Now;
  28.  
  29. Wb=App. Workbooks. Open (templetPath, Type. Missing, Type. Missing, Type.
    Missing, Type.
    Missing, Type.
    Missing, Type. Missing );
  30. Ws= (Excel. Worksheet) wb. Worksheets. get_Item (1 );
  31. }
  32.  
  33. PublicvoidExcelMethod ()
  34. {
  35. Rng=Ws. Get_Range ("B5", "C7 ");
  36. Rng. Merge (excel. XlAxisCrosses. xlAxisCrossesAutomatic );
  37. Rng. Value2="Excel2003";
  38.  
  39. Rng=Ws. Get_Range ("D8", "E11 ");
  40. Rng. MergeCells=True;
  41. Rng. Value2="Excel2003";
  42. Rng. HorizontalAlignment=Excel. XlHAlign. xlHAlignCenter;
  43. Rng. VerticalAlignment=Excel. XlVAlign. xlVAlignCenter;
  44.  
  45. Rng=Ws. Get_Range ("A1", Type. Missing );
  46. Rng. Value2=5;
  47.  
  48. Rng=Ws. Get_Range ("A2", Type. Missing );
  49. Rng. Value2=7;
  50.  
  51. For (Inti=1; I<100; I ++)
  52. {
  53. StringStrings= String. Concat ("G", I. ToString ());
  54. Rng=Ws. Get_Range (s, Type. Missing );
  55. Rng. Value2=I. ToString ();
  56. }
  57.  
  58. Tb= (Excel. TextBox) ws. TextBoxes ("text box 1 ");
  59. Tb. Text="Author";
  60.  
  61. Tb= (Excel. TextBox) ws. TextBoxes ("text box 2 ");
  62. Tb. Text="KLY. NET Blog";
  63.  
  64. Tb= (Excel. TextBox) ws. TextBoxes ("text box 3 ");
  65. Tb. Text="Date";
  66.  
  67.  
  68. Try
  69. {
  70. Tb= (Excel. TextBox) ws. TextBoxes ("text box 5 ");
  71. Tb. Text=DateTime. Now. tow.datestring ();
  72. }
  73. Catch
  74. {
  75. // Here, the Excel Process cannot be ended by using the Dispose () method. It should be used with the Kill () method of the Process.
  76. // This. Dispose ();
  77. This. KillExcelProcess ();
  78. ThrownewException ("the text box with ID \" text box 5 \ "does not exist! ");
  79. }
  80. Finally
  81. {
  82. // If an exception occurs, the Dispose () method cannot end the Excel process either.
  83. // This. Dispose ();
  84.  
  85. // If an exception occurs, you can end the Excel process here.
  86. // This. KillExcelProcess ();
  87. }
  88. }
  89.  
  90. /**////<Summary> 
  91. /// Save as an Excel file
  92. ///</Summary> 
  93. ///<ParamnameParamname="SavePath">Save path</Param> 
  94. PublicvoidSaveAsExcelFile (stringsavePath)
  95. {
  96. Wb. SaveAs (savePath, excel. XlFileFormat. xlHtml, Type. Missing, Type.
    Missing, Type. Missing, Type. Missing, excel. XlSaveAsAccessMode. xlExclusive, Type.
    Missing, Type. Missing );
  97. }
  98.  
  99. /**////<Summary> 
  100. /// End the Excel Process
  101. ///</Summary> 
  102. PublicvoidKillExcelProcess ()
  103. {
  104. Process [] myProcesses;
  105. DateTimestartTime;
  106. MyProcesses= Process. GetProcessesByName ("Excel ");
  107.  
  108. // The Excel process ID is not obtained. Currently, only the process start time can be determined.
  109. Foreach (ProcessmyProcessinmyProcesses)
  110. {
  111. StartTime=MyProcess. StartTime;
  112.  
  113. If (startTime>BeforeTime & startTime<AfterTime)
  114. {
  115. MyProcess. Kill ();
  116. }
  117. }
  118. }
  119.  
  120. /**////<Summary> 
  121. /// If the Excel operation does not cause an exception, you can use this method to end the Excel process normally.
  122. // Otherwise, use the KillExcelProcess () method to end the Excel process.
  123. ///</Summary> 
  124. PublicvoidDispose ()
  125. {
  126. Wb. Close (null, null, null );
  127. App. Workbooks. Close ();
  128. App. Quit ();
  129.  
  130. // Note: All Excel objects used here must be executed. Otherwise, the Excel process cannot be completed.
  131. If (rng! = Null)
  132. {
  133. System. Runtime. InteropServices. Marshal. ReleaseComObject (rng );
  134. Rng=Null;
  135. }
  136. If (tb! = Null)
  137. {
  138. System. Runtime. InteropServices. Marshal. ReleaseComObject (tb );
  139. Tb=Null;
  140. }
  141. If (ws! = Null)
  142. {
  143. System. Runtime. InteropServices. Marshal. ReleaseComObject (ws );
  144. Ws=Null;
  145. }
  146. If (wb! = Null)
  147. {
  148. System. Runtime. InteropServices. Marshal. ReleaseComObject (wb );
  149. Wb=Null;
  150. }
  151. If (app! = Null)
  152. {
  153. System. Runtime. InteropServices. Marshal. ReleaseComObject (app );
  154. App=Null;
  155. }
  156.  
  157. GC. Collect ();
  158. }
  159. }
  160. }

This code can solve the problem that the Excel process cannot end normally. If the host operating system is not a server version, it should end with the ntsd-c q-p pid command.

You must configure the Excel component access permission correctly in the component service. Otherwise, the Excel process cannot be completed. The specific configuration method is in the doc folder of my project; in my previous article, I introduced the web. in the config file, add the method of impersonating a user. However, I have tested that this method can access the Excel component, but it cannot end the process unless it is forcibly ended by using the Kill method. The preceding section describes how ASP. NET calls the Excel process.

  1. Analysis of Theme functions in ASP. NET development skills
  2. ASP. NET Dynamic Compilation
  3. Analysis on ASP. NET supported by Apache
  4. Introduction to ASP. NET Server standard controls
  5. Analysis on SQL Server Database Backup Recovery in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.