Query: Closing and Killing the Excel.exe from the processorhttp://forums.asp.net/t/1013312.aspx/1?Query+Closing+and+Killing+the+Excel+exe+from+the+processorTue, 01 Aug 2006 10:23:48 -040010133121357096http://forums.asp.net/p/1013312/1357096.aspx/1?Query+Closing+and+Killing+the+Excel+exe+from+the+processorQuery: Closing and Killing the Excel.exe from the processor Hi,<br> <br> i've developed a web application to retrieve the data from Sqlserver 2000 and fill it in the Dataset then, using the dataset i'l write the data into the <br> <br> Excel template file by opening the Excel and copying the excel sheet writing the data into it. After writing the data i'l save the template file as a new <br> <br> file with different name. then i'm closing the Excel books, sheets and quits the excel application. <br> <br> My problem is after closing and quiting the Excel application also the Excel.exe is still running idle in the processor, so i can't open the excel file. Only <br> <br> &nbsp;After killing the Excel.exe only i'm able to open the saved Excel File.<br> <br> In my application multiple user can access the same application so multiple Excel.exe will be running in the process. How to find out my Excel.exe and kill <br> <br> it.<br> <br> Here is my code:<br> <br> <br> using System;<br> using Excel;<br> using System.IO;<br> using System.Net;<br> using System.Collections;<br> using System.ComponentModel;<br> using System.Configuration;<br> using System.Data;<br> using System.Data.SqlClient;<br> using System.Drawing;<br> using System.Diagnostics;<br> using System.Web;<br> using System.Web.SessionState;<br> using System.Web.UI;<br> using System.Web.UI.WebControls;<br> using System.Web.UI.HtmlControls;<br> using System.Runtime.InteropServices;<br> using CrystalDecisions.CrystalReports.Engine;<br> using CrystalDecisions.Shared;<br> using RealImageReports.Reports;<br> using RealImageReports.Include;<br> <br> namespace RealImageReports<br> {&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; public class ExcelWriter<br> &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Excel.ApplicationClass _excel = new Excel.ApplicationClass();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Excel.Workbooks oBooks;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Excel._Workbook oBook;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Excel.Worksheet ws;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public void WorkSheetOpen(string path, string filename)<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBooks = _excel.Workbooks;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBook = oBooks.Open(path &#43; filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _excel.Visible = true;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public void WriteData(string path, string filename, DataSet _ds, string theatre, string Ads, string STime, string ETime)<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws =(Worksheet) oBook.Worksheets[1];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Activate();<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Copy(ws, Type.Missing);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Name = theatre;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws=(Worksheet) oBook.Worksheets[theatre];<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Activate(); <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FillHeadersParam(theatre, Ads, STime, ETime);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FillDataRows(ws, _ds);&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public void FillHeadersParam(string theatre, string Ads, string STime, string ETime)<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AddItemToSpreadsheet(5, 2, ws, theatre);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AddItemToSpreadsheet(5, 6, ws, STime);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AddItemToSpreadsheet(6, 2, ws, Ads);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AddItemToSpreadsheet(6, 6, ws, ETime);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AutoFitColumn(ws, 2);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AutoFitColumn(ws, 5);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public void WorkSheetClose() <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws =oBook.Sheets.get_Item(1) as Excel.Worksheet;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Activate();<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ws.Visible = Excel.XlSheetVisibility.xlSheetVeryHidden;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBook.Save(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBook.Close(XlSaveAction.xlSaveChanges,Type.Missing, Type.Missing);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBooks.Close();&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; oBooks.Application.Quit();<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _excel.Quit();<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Marshal.ReleaseComObject(ws);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Marshal.ReleaseComObject(oBook);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Marshal.ReleaseComObject(oBooks);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Marshal.ReleaseComObject(_excel);<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp; }<br> <br> }<br> <br> <br> keenly waiting for your reply.<br> <br> Regards, 2006-08-01T10:23:48-04:00