Why does Excel not close after building PDF?

2019-07-15 03:53发布

I use this code to convert an excel file to PDF. The problem is that the Excel process is not close. What am I missing?

        protected void Indexchanged_ConvertPDF(Object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        xlApp.DisplayAlerts = false;
        xlApp.Visible = false;
        xlApp.ScreenUpdating = false;

        string path = CertificadosPresion.SelectedRow.Cells[0].Text;
        string CertName = CertificadosPresion.SelectedDataKey.Value.ToString();

        Workbook xlWorkbook = xlApp.Workbooks.Open(path);

        xlWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, @"C:\pdf\" + SALESID.Text + "_CertPres.pdf", 0, false, true);

        xlWorkbook.Close();
        xlApp.Quit();
        xlApp = null;
        xlWorkbook = null;

        DisposeCOMObject(xlWorkbook);
        DisposeCOMObject(xlApp);
    }

1条回答
贪生不怕死
2楼-- · 2019-07-15 04:43

It is not that easy as one might think. There is a nice article losing some words about this topic:

http://devcity.net/PrintArticle.aspx?ArticleID=239

But other than that I think that this question might be a duplicate of Closing Excel Application Process in C# after Data Access. There are a lot of potential solutions for your problem.

查看更多
登录 后发表回答