Workaround on the process of calling Excel components in ASP.NET

xiaoxiao2021-03-18  180

The common solution is the following code for adjustment Excel components in ASP.NET.

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 in the case of the configuration, the premise is that if there is an exception when operating Excel, if there is an abnormality, the Excel process will not end (such as: Quote a reference in the Excel file "Exceptions in HRESULT: 0x800A03ec.") Will appear when there is no text box in the middle.

Using

System;

Using

System.Diagnostics;

Using

Excel

=

Microsoft.Office.Interop.Excel;

Namespace

Exceltest

{/ ** ////

/// Excel's summary description.

/// public class Excel {private datetime beforetime; // Excel launched prior time prior time prior time prior time prior time prior time prior time prior time prior time prior time prince Excel.TextBox TB; Public Excel (String TempletPath) {// instantiates an Excel Application object and makes it visible beforeetime = datetime.now; app = new excel.Applicationclass (); app.visible = true; afterTime = DateTime. Now; 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; WS = (Excel.Worksheet) WB.Worksheets.get_Item (1); PUBLIC VOID EXCELMETHOD () {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 (INT i = 1; i <100; i ) {string s = 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 = "Japan"; try {TB = (Excel.TextBox) ws.textboxes ("text box 5"); tb.text = datetime.now.toshortdateString ();} catch {// Here you with the dispose () method Can't use the Excel process, all still use process's Kill () method to use // this.dispose (); this.killexcelprocess (); throw new exception ("There is no ID to /" text box 5 / "text box! ");} Finally {// If there is an abnormality, the Dispose () method is also over the EXCEL process // this.dispose (); // If an exception occurs, you can also end the Excel process // this. KillexcelProcess ();}} / ** //// // / Save as Excel file /// /// save the path public void saveaseXcelfile (STRING SavePath) {wb.saveas (SavePath, Excel.xlfileFormat .XLHTML, TYPE.MISSING, TYPE.MISSING, Excel.xlsaveasaccessmode.xlexClsive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing;} / ** ////

/// End Excel process /// public void killexcelprocess () {process [] MyProcesses; DateTime StartTime Download Adobe Reader D ;TIME STARTTIME

myProcesses = Process.GetProcessesByName ( "Excel"); // get Excel process ID, process start time is determined only temporarily foreach (Process myProcess in myProcesses) {startTime = myProcess.StartTime; if (startTime> beforeTime && startTime /// If the operation of Excel does not trigger an exception, use this method to end the Excel process normally // / Otherwise, use KillexcelProcess ( Method to end Excel process /// public void dispose () {wb.close (null, null, null); app.quoocks.close (); app.quit (); // Note: Here All Excel objects to be implemented, otherwise the Excel process if (RNG! = Null) {system.Runtime.InteropServices.Marshal.RuntimeComobject (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.RunTimeComobject (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 properly. If the host operating system is not a server version, then you will end with the NTSD-C Q -P PID command. There is also a problem with the configuration of Excel component access, so you must configure it in the component service, otherwise you can't end the Excel process, the specific configuration method is under the DOC folder of my project; introduced it in front of my article. The web.config file is added to the method of pretending the user, but the method I have discovered this method although I can access the Excel component, but I can't end the process unless you overcome with the Kill method. Click here to download Exceltest.rar Source: http://www.cnblogs.com/lingyun_k/archive/2005/11/08/271796.html

转载请注明原文地址:https://www.9cbs.com/read-129781.html

New Post(0)