Closing and Disposing Excel Application Process in C#
-
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
-
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
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:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer