1

I have been trying to save a excel file as pdf format. every time I run the program, a excel job alives in Task-Manager. I can save a excel file as pdf file but a excel job isn't removed. It would be really helpful if someone help me out this situation. thank you.

using Excel = Microsoft.Office.Interop.Excel;
main{
 var bookToPdf = new MyExcelBook();
 bookToPdf.SaveAsPDF(@"C:\test.xlsx", @"C:\test.pdf");
}

namespace MyExcel{
public class MyExcelBook{
 public  void SaveAsPDF(string excelPath, string pdfPath)
 {
    Excel.Application application = new Excel.Application();
    Excel.Workbooks workbooks = application.Workbooks;
    Excel.Workbook workbook = workbooks.Open(excelPath); ;
    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];

    workbook.ExportAsFixedFormat2 ( 
        Filename: pdfPath,
        Type:Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF);
    worksheet.Cells.Clear();
    workbook.Close(SaveChanges: false);
 
    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
    worksheet = null;
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
    workbook = null;
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
    workbooks = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();

    application.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
    application = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    }
}
}
Lemonade
  • 21
  • 4
  • 1
    Did you try `application.Quit`? https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.application?view=excel-pia#methods – Flydog57 Aug 03 '23 at 05:37
  • Thank you for your comment.I have tried. I wrote a code "application.Quit();", but a excel job does somehow remain in my Task-Manager. I am stuck very badly. – Lemonade Aug 03 '23 at 06:35
  • This looks correct to me. (But the unnecessary GC and ReleaseComObject commands. Amd also setting the variables to null is does nothing usefull in this method) Are you shure that the remaining Excel instance is the one created by this code? – Rene Niediek Aug 03 '23 at 08:30
  • Thank you for your comment Mr/Ms Rene. I am sure that the remaining Excel instance is created by this code. The reference is here. https://social.msdn.microsoft.com/Forums/ja-JP/5deec897-a897-404b-a610-f7d894fde1b3/office?forum=officesupportteamja. This article is official Microsoft although it is written in Japanse. I think the article is way too old. So Mayby some of the code are unnecessary........ mmmm I am stuck.... – Lemonade Aug 04 '23 at 01:34

2 Answers2

1

I have found a solution finally. Thank you so much you guys who contributed this question.

Solution has 3 steps.

Step1: Set "Relese" at Configuration manager.

"Build"tab -> Configuration Manager -> Active solution configuration -> Select "Relese"

Step2: Uncheck "Suppress JIT optimization" at Debug Option.

"Tool"tab -> Select "Debuging" -> select "General" -> untick "Suppress JIT OPtimization"

Step3: Write codes written below.

namespace MyExcel{
public class MyExcelBook{
 public  void SaveAsPDF(string excelPath, string pdfPath)
 {
    Excel.Application application = new Excel.Application()
        { Visible = false };
    Excel.Workbooks workbooks = application.Workbooks;
    Excel.Workbook workbook = workbooks.Open(excelPath); ;
    Excel.Sheets sheets = application.Worksheets;
    Excel.Worksheet worksheet = (Excel.Worksheet)sheets[1];

    workbook.ExportAsFixedFormat2 ( 
                Filename: pdfPath,
                Type:Excel.XlFixedFormatType.xlTypePDF);
    
    workbook.Close(SaveChanges: false);
    application.Quit();
 }
}

These lines are all I needes. It is quite simple after a solution has found but I spend days to come here.....

reference are also important. I don't want anybody to struggle to solve this problem, so strongly recommended to read these article written below...

*1 Understanding garbage collection in .NET *2 How do I properly clean up Excel interop objects?

Thank you so much.

Lemonade
  • 21
  • 4
  • Why this works is because you're using single dots. With Office Interop it doesn't like double dots... https://stackoverflow.com/questions/13069153/eliminating-use-of-2-dots-when-using-excel-interop-com-objects-c-sharp * Edit, nice one you referenced the canonical guide! – Jeremy Thompson Aug 04 '23 at 09:01
  • It is interesting informatin that interop preffers "Single Dot ". Thank you for letting me know that!!!!! – Lemonade Aug 08 '23 at 00:29
0

You need to ReleaseComObject all your other objects too.

The code below uses late binding, but you can try adding the ReleaseComObject calls on your early-bound code.

using System.Runtime.InteropServices;

var xlsFilename = "c:\\Junk\\Junk.xlsx";
var pdfFilename = "c:\\Junk\\Junk.pdf";

SaveAsPdf(xlsFilename, pdfFilename);

Console.WriteLine("FINISHED");
Console.ReadKey();


void SaveAsPdf(string excelPath, string pdfPath)
{
    Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
    dynamic application = Activator.CreateInstance(ExcelType);
    dynamic workbooks = application.Workbooks;
    dynamic workbook = workbooks.Open(excelPath);        
    workbook.ExportAsFixedFormat(0, pdfFilename);    
    application.Quit();
    Marshal.ReleaseComObject(workbooks);
    Marshal.ReleaseComObject(workbook);
    Marshal.ReleaseComObject(application);    
}
SSS
  • 4,807
  • 1
  • 23
  • 44
  • Also if your program runs excel for the first time on the PC, it can be a problem, because there are some initial config screens that pop up, the first time Excel is run. – SSS Aug 03 '23 at 07:17
  • Thank you for your help Mr/Ms SSS. I really appriciate your information. It is not first time for the PC to runs Excel. I have been using excel for years with my PC. So that may not reason.... the reason why excel job remains is my code....... I will try the code that you write for me sometimes soon. Thank you. – Lemonade Aug 04 '23 at 01:42