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. .NET (Core and Framework)
  4. Closing and Disposing Excel Application Process in C#

Closing and Disposing Excel Application Process in C#

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpsysadmin
2 Posts 2 Posters 5 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.
  • P Offline
    P Offline
    Pramod_Kumar1
    wrote on last edited by
    #1

    Excel.Application oExcel = new Excel.Application();
    oExcel.Visible = true;
    Excel.Workbooks oWorkbooks = oExcel.Workbooks;
    string Pfad = "C:\test.xlsx";
    Excel.Workbook oWorkbook = oWorkbooks.Open(Pfad);

                Excel.Worksheet oSheet = oWorkbook.Sheets\[1\];
                
                oSheet.Cells\[1, 1\] = "Test";
                
                oSheet.SaveAs("C:\\test.xlsx");
    
                GC.Collect();
                GC.WaitForPendingFinalizers();
                        
                Marshal.FinalReleaseComObject(oSheet);
                oWorkbook.Close();                
                Marshal.FinalReleaseComObject(oWorkbook);
                oExcel.Workbooks.Close();                
                Marshal.FinalReleaseComObject(oWorkbooks);
                oExcel.Application.Quit();                
                Marshal.FinalReleaseComObject(oExcel);
                oExcel = null;
    

    Above code is not working on server. Office is not activated on server so this is reason to not closing it or any other issue, please help me to solve this problem

    Richard DeemingR 1 Reply Last reply
    0
    • P Pramod_Kumar1

      Excel.Application oExcel = new Excel.Application();
      oExcel.Visible = true;
      Excel.Workbooks oWorkbooks = oExcel.Workbooks;
      string Pfad = "C:\test.xlsx";
      Excel.Workbook oWorkbook = oWorkbooks.Open(Pfad);

                  Excel.Worksheet oSheet = oWorkbook.Sheets\[1\];
                  
                  oSheet.Cells\[1, 1\] = "Test";
                  
                  oSheet.SaveAs("C:\\test.xlsx");
      
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                          
                  Marshal.FinalReleaseComObject(oSheet);
                  oWorkbook.Close();                
                  Marshal.FinalReleaseComObject(oWorkbook);
                  oExcel.Workbooks.Close();                
                  Marshal.FinalReleaseComObject(oWorkbooks);
                  oExcel.Application.Quit();                
                  Marshal.FinalReleaseComObject(oExcel);
                  oExcel = null;
      

      Above code is not working on server. Office is not activated on server so this is reason to not closing it or any other issue, please help me to solve this problem

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Considerations for server-side Automation of Office[^]

      Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

      There are various ways to create or modify Excel spreadsheets on the server without using Office interop. For example:

      • EPPlus[^];
      • ClosedXML[^];
      • The OpenXML SDK[^];

      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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