简体   繁体   中英

Exporting Excel file to PDF - getting stuck trying to close Excel file

I have a function that takes a url to an Excel file, and then exports it as a pdf:

public static void ExportFromExcel(string url)
{
    // Create COM Objects
    Application excelApplication = new Application();
    Workbook excelWorkbook = 
        excelApplication.Workbooks.Open(HttpContext.Current.Server.MapPath(url));

    try
    {
        // Call Excel's native export function
        excelWorkbook.ExportAsFixedFormat(
            XlFixedFormatType.xlTypePDF, 
            HttpContext.Current.Server.MapPath(url.Replace(".xlsx", ".pdf")));
    }
    catch (Exception ex)
    {

    }
    finally
    {
        // Close the workbook, quit Excel, and clean up regardless of the results
        excelWorkbook.Close();    // HANGS AND FAILS HERE
        excelApplication.Quit();

        excelApplication = null;
        excelWorkbook = null;
    }
}

What I'm doing is creating an Excel file, and then immediately creating a pdf from that file:

using (FileStream fs = File.Create(HttpContext.Current.Server.MapPath(filePath)))
{
    byte[] data = pck.GetAsByteArray();
    fs.Write(data, 0, data.Length);
}

Pdf.ExportFromExcel(filePath);

It will create the Excel file just fine, and it will also create the pdf. But the page hangs for a long time, and then gives me the error at excelWorkbook.Close(); in my pdf code:

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

The files actually get created though. But if I go to the file directory, there is a temporary Excel file as well (my Excel filename with the ~$ in front of it).

If I try and delete my Excel file, I get an error saying the file is being used by another process, and I can't delete my Excel file until I kill the EXCEL.EXE in the Task Manager.

So for some reason, my Excel file won't close after it finishes creating the pdf. Am I doing something wrong in my code?

Using Interop on the server (including ASP.NET/IIS!) is NOT supported by MS - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

Since Windows Vista MS introduced several security-related measures which prevent a Windows Service (like ASP.NET on IIS) from doing "desktop-like" things (accessing printers, accessing network shares etc.)... which means you would have to circumvent several security measures to get it to work (NOT recommended!).

To deal with Excel files without any need for Office etc. in a server-scenario there are several options (free and commercial) out there:

I can recommend Aspose.Cells and Flexcel - both come with PDF export functionality... didn't try SpreadsheetGear but hear+read lots of good things about it...

Free options (though for the newer xlsx format only, not sure whether they support PDF export!) are for example OpenXML 2 from MS and EPPlus .

I know that this "offically" has been answered as "not supported". That said, I ran into this same issue.

The actual exception is kind of a red herring. The real issue is Permissions.

You can either set your web service to run under the identity of a user with adequate permissions (not always a good idea) OR you can go into DCOMCNFG and alter the permissions of the Excel app so that your System user has the right access.

I hope that this helps someone in the future.

  1. Install The 2007 Microsoft Office Suite Service Pack 3 (SP3) http://www.microsoft.com/en-in/download/details.aspx?id=27838

  2. Install 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS http://www.microsoft.com/en-in/download/details.aspx?id=7

Mandatory : Set Microsoft XPS Document Writer as default printer in server. Because ExportAsFixedFormat function executes Microsoft XPS Document Writer to convert Excel to PDF.

This is working for me.

You can use following code to export excel file to pdf:

// Saving an XLS file in Aspose.Pdf xml format
Workbook wb = new Workbook();
wb.Open("C:\\book1.xls);
wb.Save("C:\\xls2pdf.xml", FileFormatType.AsposePdf);

// Converting XLS file to PDF through Aspose.Pdf using Aspose.Pdf xml file as a medium
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML("c:\\xls2pdf.xml", null);
pdf.Save("C:\\xls2pdf.pdf");


You can download Aspose.pdf from the following link
http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/category1184.aspx

You can download Aspose.cells from the following link
http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/category1184.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM