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.
- 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();
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:
- UsingSystem;
- UsingSystem. Diagnostics;
- Usingexcel=Microsoft. Office. Interop. Excel;
-
- NamespaceExcelTest
- {
- /**////<Summary>
- /// Summary in Excel.
- ///</Summary>
- PublicclassExcel
- {
- PrivateDateTimebeforeTime; // time before Excel start
- PrivateDateTimeafterTime; // time after Excel startup
-
- Excel. Applicationapp;
- Excel. Workbookwb;
- Excel. Worksheetws;
- Excel. Rangerng;
- Excel. TextBoxtb;
-
- PublicExcel (stringtempletPath)
- {
- // Instantiate an ExcelApplication object and make it visible
- BeforeTime=DateTime. Now;
- App=Newexcel. ApplicationClass ();
- App. Visible=True;
- AfterTime=DateTime. Now;
-
- Wb=App. Workbooks. Open (templetPath, Type. Missing, Type. Missing, Type.
Missing, Type.
Missing, Type.
Missing, Type. Missing );
- Ws= (Excel. Worksheet) wb. Worksheets. get_Item (1 );
- }
-
- PublicvoidExcelMethod ()
- {
- Rng=Ws. Get_Range ("B5", "C7 ");
- Rng. Merge (excel. XlAxisCrosses. xlAxisCrossesAutomatic );
- Rng. Value2="Excel2003";
-
- Rng=Ws. Get_Range ("D8", "E11 ");
- Rng. MergeCells=True;
- Rng. Value2="Excel2003";
- Rng. HorizontalAlignment=Excel. XlHAlign. xlHAlignCenter;
- Rng. VerticalAlignment=Excel. XlVAlign. xlVAlignCenter;
-
- Rng=Ws. Get_Range ("A1", Type. Missing );
- Rng. Value2=5;
-
- Rng=Ws. Get_Range ("A2", Type. Missing );
- Rng. Value2=7;
-
- For (Inti=1; I<100; I ++)
- {
- StringStrings= String. Concat ("G", I. ToString ());
- Rng=Ws. Get_Range (s, Type. Missing );
- Rng. Value2=I. ToString ();
- }
-
- Tb= (Excel. TextBox) ws. TextBoxes ("text box 1 ");
- Tb. Text="Author";
-
- Tb= (Excel. TextBox) ws. TextBoxes ("text box 2 ");
- Tb. Text="KLY. NET Blog";
-
- Tb= (Excel. TextBox) ws. TextBoxes ("text box 3 ");
- Tb. Text="Date";
-
-
- Try
- {
- Tb= (Excel. TextBox) ws. TextBoxes ("text box 5 ");
- Tb. Text=DateTime. Now. tow.datestring ();
- }
- Catch
- {
- // Here, the Excel Process cannot be ended by using the Dispose () method. It should be used with the Kill () method of the Process.
- // This. Dispose ();
- This. KillExcelProcess ();
- ThrownewException ("the text box with ID \" text box 5 \ "does not exist! ");
- }
- Finally
- {
- // If an exception occurs, the Dispose () method cannot end the Excel process either.
- // This. Dispose ();
-
- // If an exception occurs, you can end the Excel process here.
- // This. KillExcelProcess ();
- }
- }
-
- /**////<Summary>
- /// Save as an Excel file
- ///</Summary>
- ///<ParamnameParamname="SavePath">Save path</Param>
- PublicvoidSaveAsExcelFile (stringsavePath)
- {
- Wb. SaveAs (savePath, excel. XlFileFormat. xlHtml, Type. Missing, Type.
Missing, Type. Missing, Type. Missing, excel. XlSaveAsAccessMode. xlExclusive, Type.
Missing, Type. Missing );
- }
-
- /**////<Summary>
- /// End the Excel Process
- ///</Summary>
- PublicvoidKillExcelProcess ()
- {
- Process [] myProcesses;
- DateTimestartTime;
- MyProcesses= Process. GetProcessesByName ("Excel ");
-
- // The Excel process ID is not obtained. Currently, only the process start time can be determined.
- Foreach (ProcessmyProcessinmyProcesses)
- {
- StartTime=MyProcess. StartTime;
-
- If (startTime>BeforeTime & startTime<AfterTime)
- {
- MyProcess. Kill ();
- }
- }
- }
-
- /**////<Summary>
- /// If the Excel operation does not cause an exception, you can use this method to end the Excel process normally.
- // Otherwise, use the KillExcelProcess () method to end the Excel process.
- ///</Summary>
- PublicvoidDispose ()
- {
- Wb. Close (null, null, null );
- App. Workbooks. Close ();
- App. Quit ();
-
- // Note: All Excel objects used here must be executed. Otherwise, the Excel process cannot be completed.
- If (rng! = Null)
- {
- System. Runtime. InteropServices. Marshal. ReleaseComObject (rng );
- Rng=Null;
- }
- If (tb! = Null)
- {
- System. Runtime. InteropServices. Marshal. ReleaseComObject (tb );
- Tb=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 ();
- }
- }
- }
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.
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET