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();
    }
}
}
4
  • 1
    Did you try application.Quit? learn.microsoft.com/en-us/dotnet/api/… Commented Aug 3, 2023 at 5: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. Commented Aug 3, 2023 at 6: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? Commented Aug 3, 2023 at 8: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. social.msdn.microsoft.com/Forums/ja-JP/…. 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.... Commented Aug 4, 2023 at 1:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Why this works is because you're using single dots. With Office Interop it doesn't like double dots... stackoverflow.com/questions/13069153/… * Edit, nice one you referenced the canonical guide!
It is interesting informatin that interop preffers "Single Dot ". Thank you for letting me know that!!!!!
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);    
}

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.