Excel Doest quit after operations.
Results 1 to 3 of 3

Thread: Excel Doest quit after operations.

  1. #1
    Senior Member codenamevirus's Avatar
    Join Date
    Jun 2005
    Location
    Faridabad, Haryana, India
    Posts
    298

    Exclamation Excel Doest quit after operations.

    Hi

    I created a web method, that would export my data on the page into an excel document.

    The runs perfectly well, but the problem is, that after the operations are performed and fininshed, excel doesnt quit and an instance of excel.exe remains there (saw it through the task manager). Now, every time the web method executes, a new instance is created of excel which is not quit later...so when the method got called 10 times, I was able to see 10 instances of execl...so how do I get rid of this problem and close excel after every time the method is called.

    Here's the code:

    [WebMethod]
    [ScriptMethod]
    public void ExportToExcel(GNode[][] ExpandedNodes, GNode[][] AllNodes, GNode[] Sender, string[] TableName, DataFilter[] Filter, DataSortOrder[] SortExpression, int[] CurentPage, int[] PageSize)
    {

    DataTable Displayed_DataTable = null;
    Displayed_DataTable = DisplayedDataTableSimulator(ExpandedNodes, AllNodes, Sender, TableName, Filter, SortExpression, CurentPage, PageSize);
    int TableColumnCount = Displayed_DataTable.Columns.Count;
    int TableRowCount = Displayed_DataTable.Rows.Count;
    string objDirPath = Server.MapPath("tempDocs");
    Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbook wb = null;
    try
    {
    string fileName = objDirPath + "\\Mdd.xls";
    wb = xl.Workbooks.Open(fileName, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value, Missing.Value, true, false, Missing.Value, false, false, false);
    DataColumn[] dc = new DataColumn[TableColumnCount];
    DataRow[] dr = new DataRow[TableRowCount];
    Displayed_DataTable.Columns.CopyTo(dc, 0);
    Displayed_DataTable.Rows.CopyTo(dr, 0);
    Microsoft.Office.Interop.Excel.Sheets xlsSheets = wb.Sheets;
    Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsSheets[1];
    ExcelWorkSheet.Rows.Clear();
    int NoColumnInSheet = (TableColumnCount - 1) / 2;

    for (int i = 0, j = 0; i < TableColumnCount - 1; i = i + 2, j++)//Get The Column Header
    {
    char c = (char)(65 + j);
    Microsoft.Office.Interop.Excel.Range ExcelHeader = (Microsoft.Office.Interop.Excel.Range)ExcelWorkSheet.get_Range(c + "1:" + c + "1", Type.Missing);
    ExcelHeader.Cells.Value2 = dc[i].ColumnName.ToString();
    wb.Save();
    }
    object beforeRow = Type.Missing;
    for (int i = 2; i <= TableRowCount + 1; i++)// fill the cells with data
    {

    for (int j = 0, jc = 1; j < TableColumnCount - 1; j = j + 2, jc++)
    {
    char c = (char)(65 + (jc - 1));
    Microsoft.Office.Interop.Excel.Range ExcelHeader = (Microsoft.Office.Interop.Excel.Range)ExcelWorkSheet.get_Range("" + (c) + (i) + ":" + (c) + (i), Type.Missing);
    ExcelHeader.Cells.Value2 = dr[i - 2][dc[j].ColumnName.ToString()].ToString();
    wb.Save();
    }
    }
    object Save_changes = true;
    wb.Close(Save_changes, fileName, Missing.Value);

    }
    finally
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
    wb = null;
    try
    {
    xl.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);
    }
    catch (Exception ex) { }
    finally { xl = null; }
    GC.Collect();
    }

    }
    Please seee....

    Thanks
    CodeNameVirus

  2. #2
    AO's Filibustier Cheap Scotch Ron's Avatar
    Join Date
    Nov 2008
    Location
    Swamps of Jersey
    Posts
    378
    While not elegant., did you try a brute force kill?
    e.g.

    Process[] procs = Process.GetProcessesByName("EXCEL");
    foreach (Process p in procs)
    {
    int baseAdd = p.MainModule.BaseAddress.ToInt32();
    //oXL is Excel.ApplicationClass object
    if (baseAdd == oXL.Hinstance)
    p.Kill();
    }
    Last edited by Cheap Scotch Ron; November 5th, 2008 at 08:36 PM.

  3. #3
    Senior Member codenamevirus's Avatar
    Join Date
    Jun 2005
    Location
    Faridabad, Haryana, India
    Posts
    298
    Quote Originally Posted by Cheap Scotch Ron View Post
    While not elegant., did you try a brute force kill?
    e.g.

    Process[] procs = Process.GetProcessesByName("EXCEL");
    foreach (Process p in procs)
    {
    int baseAdd = p.MainModule.BaseAddress.ToInt32();
    //oXL is Excel.ApplicationClass object
    if (baseAdd == oXL.Hinstance)
    p.Kill();
    }
    Tried something similar to this. I got the process Id of the exact instance rather than just Excel, coz if there were more than one instance of excel open on the machine, and some user is using those instances, than the chances screwing him by accidentally closing his excel is more.

    This thing worked locally on my machine...but when I hosted the code on the Internet server, it didnt kill the process over there...do you think, I need to give some permissions on the server machine?
    CodeNameVirus

Similar Threads

  1. Creating an Excel query...
    By RebelToTheEnd in forum General Computer Discussions
    Replies: 0
    Last Post: January 11th, 2005, 03:01 PM
  2. Secure Spreadsheets
    By IcSilk in forum Newbie Security Questions
    Replies: 7
    Last Post: July 5th, 2004, 09:23 PM
  3. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  4. Explore ways to pass a Microsoft Excel file to the client side
    By NullDevice in forum Programming Security
    Replies: 2
    Last Post: October 15th, 2003, 10:06 PM
  5. Microsoft Excel 2002 Tutorial
    By One Who Watches in forum Other Tutorials Forum
    Replies: 0
    Last Post: July 20th, 2003, 12:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •