wupeijun Posted April 7, 2003 Posted April 7, 2003 (edited) If use workbook object then unable kill the excel process code as follows: public void CreateExcel(DataView dv,string path,string title1) { Excel.Application excel; try { excel =new Excel.Application(); } catch { Response.Write("·þÎñÆ÷¶ËExcel·±Ã¦£¡ÇëÉÔºó!"); return; } object oMissing =System.Reflection.Missing.Value; excel.UserControl =false; excel.DisplayAlerts=false; excel.AlertBeforeOverwriting=false; Excel._Workbook book =excel.Workbooks.Add(oMissing); int rowIndex =2; int colIndex =0; excel.Cells[1,1] =title1; foreach(DataColumn dc in dv.Table.Columns) { colIndex++; excel.Cells[rowIndex,colIndex] =dc.ColumnName; ((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8; } foreach(DataRow row in dv.Table.Rows) { rowIndex++; colIndex =0; foreach(DataColumn dc in dv.Table.Columns) { colIndex ++; excel.Cells[rowIndex,colIndex] = row[dc.ColumnName].ToString(); ((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8; } } Excel.Sheets sheets =book.Worksheets; Excel._Worksheet worksheet =(Excel._Worksheet)sheets.get_Item(1); Excel.Range range; range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,14]); range.MergeCells =true;//ºÏ²¢ range.HorizontalAlignment =Excel.XlHAlign.xlHAlignCenter;//¾ÓÖÐ range.Font.Bold =true; range.Font.Size =14; //»_Ïß range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,colIndex]); range.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle =Excel.XlLineStyle.xlLineStyleNone; range.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle =Excel.XlLineStyle.xlLineStyleNone; range.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous; range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle =Excel.XlLineStyle.xlContinuous; //À_¿í±í¸ñ range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,1]); range.ColumnWidth =25.8; range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,1]); range.Font.Bold =true; range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[2,colIndex]); range.Font.Bold =true; book.Saved =true; worksheet.SaveAs(path,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing); this.NAR(oMissing); this.NAR(sheets); this.NAR(worksheet); book.Close(false,null,null); this.NAR(book); excel.Quit(); this.NAR(excel); GC.Collect(); } private void NAR(object o) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(o); } catch{} o=null; } Edited April 7, 2003 by Robby Quote
Moderators Robby Posted April 7, 2003 Moderators Posted April 7, 2003 Where and how frequently are you calling CreateExcel() ? Quote Visit...Bassic Software
wupeijun Posted April 8, 2003 Author Posted April 8, 2003 haw-haw.I know!this's problem of web Application,if use winform then no problem! thank you! Robby Quote
cutiti Posted May 21, 2003 Posted May 21, 2003 Does that mean Excel can't be closed if running within web application? I 'm experiencing the similar problem where Excel process is still running. Many thanks. Quote
Moderators Robby Posted May 21, 2003 Moderators Posted May 21, 2003 cutiti, what does your code look like? Quote Visit...Bassic Software
Madz Posted May 21, 2003 Posted May 21, 2003 My Dear at the end of your applicaton your must 1- Close the document which you opened or created through code 2- Kill the excel process by sending command Excel.Application.Close or something like this in aplication object methods other wise your object would remain in memory. and in future development please remember one thing whenever you try to call any Ofifce asseomblies, remeber to mention its CLOSE methods Quote The one and only Dr. Madz eee-m@il
MCSD2X Posted June 27, 2003 Posted June 27, 2003 (edited) Nice try Madz. It would be nice if this issue was as simple as calling "Excel.Application.Close or something like this". You are trying to trivialize an issue that is much deeper than what it appears. When you use managed code to access an Office Application, a Runtime Callable Wrapper is created that keeps a reference count on the COM object (in this case, an office application). Therefore, all references must be released on the RCW or the COM object will not quit (even when calling the close or quit method of the object). For detailed information on this subject research Interop Services for Microsoft Office. To release references on the RCW lookup "System.Runtime.InteropServices.Marshal.ReleaseComObject(o)". There are several good articles published on the web that address this issue. Also, you may not see the Excel object terminate immediately in the task manager. The GC will terminate it in its own time (usually within 10 minutes). Edited June 27, 2003 by MCSD2X Quote
MikeDraper Posted September 2, 2003 Posted September 2, 2003 COM Interoperability and Windows Forms I've found that the Excel application object is bound to the main application thread when used in a windows form. This is not the case in a command prompt thread. You'll notice that the close & quit code *works* in a command prompt application, but in a windows form, it sticks around until the window closes. //This code, flat out.. does not work. It doesn't quit the Excel object in a windows form. writeExcel(); workbook.Close(true, Environment.CurrentDirectory + "exc" + DateTime.Now.Ticks.ToString() + ".xls", false); exc.Quit(); Marshal.ReleaseComObject(exc); //This is supposed to trash the object. It Lies! /* I've tried with and without this. GC.WaitForPendingFinalizers(); */ GC.Collect(); // This *should* force the GC to clean up.. thus not requiring the 10 minute wait. Since this fires, exc must not be getting trashed. So I stuffed writeExcel() into its own thread.. If I call txlThread.Abort() on the thread, it works.. but jesus isn't that kind of messy? :\ Also, this would necessitate me setting up an event for when writeExcel() is through.. Otherwise you can kill the thread before its done it's job. There Has to be a more graceful way to do this. Also .. Did anyone notice that when you define the excel Application object, you get an Ambiguous reference? I have to use Excel.ApplicationClass Excel.WorkbookClass etc... I'm wondering if there is a scope issue here at work behind the scenes that isn't getting handled well. Quote
Leaders dynamic_sysop Posted September 2, 2003 Leaders Posted September 2, 2003 have you looked into the Office XP Primary Interop Assemblies? they are designed to make office projects in vs ( such as Excel , Word etc... ) run better, this article explains it better... Office Xp PIA's Quote
AlexanderByndyu Posted June 8, 2009 Posted June 8, 2009 You can find solutions here http://blog.byndyu.ru/2009/05/excelexe-interopexcel.html Quote
Recommended Posts