Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Excel remains in memory

Excel remains in memory

Scheduled Pinned Locked Moved C#
csharpvisual-studiocomtoolsperformance
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaurGurpreet
    wrote on last edited by
    #1

    I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.

    Gurpreet

    L M 3 Replies Last reply
    0
    • K KaurGurpreet

      I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.

      Gurpreet

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi I had the same problem

      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();//excel will close only after calling these methods twice
      GC.WaitForPendingFinalizers();

      after setting _objAppln=null; worked for me greets

      K 1 Reply Last reply
      0
      • L Lost User

        Hi I had the same problem

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();//excel will close only after calling these methods twice
        GC.WaitForPendingFinalizers();

        after setting _objAppln=null; worked for me greets

        K Offline
        K Offline
        KaurGurpreet
        wrote on last edited by
        #3

        Explicitly calling GC.collect is not recommended. Is there any other good way to handle it?

        Gurpreet

        1 Reply Last reply
        0
        • K KaurGurpreet

          I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.

          Gurpreet

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          Try releasing the Excel Application COM object by using the following code:

          try
          {
          if (objAppln!= null)
          System.Runtime.InteropServices.Marshal.ReleaseComObject(objAppln);
          }
          catch (Exception)
          {
          // ...
          }
          finally
          {
          objAppln = null;
          }

          1 Reply Last reply
          0
          • K KaurGurpreet

            I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.

            Gurpreet

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            KaurGurpreet wrote:

            What is wrong in the above code.

            I'm missing the code to close the workbook and exit the Excell application. Meh, they're there, just overlooked them. This kb-article[^] might help. The only other alternative[^] that I can offer;

            Marshal.ReleaseComObject(workBook);

            I are Troll :suss:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups