Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel ExportAsFixedFormat PDF

Tags:

c#

excel-2007

I can successfully take an excel file and export it as a PDF file in c#

private static void ExportWorkbookToPDF(string workbook, string output)
{
    if (string.IsNullOrEmpty(workbook) || string.IsNullOrEmpty(output))
    {
        throw new NullReferenceException("Cannot create PDF copy " +
            "from empty workbook.");
    }

    Application excelApplication = new Application();
    excelApplication.ScreenUpdating = false;
    excelApplication.DisplayAlerts = false;
    excelApplication.Visible = false;

    Workbook excelWorkbook = excelApplication.Workbooks.Open(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
        "\\" + workbook);

    if (excelWorkbook == null)
    {
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;

        throw new NullReferenceException("Cannot create new excel workbook.");
    }

    try
    {
        excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, 
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
            "\\" + output);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
    finally
    {
        excelWorkbook.Close();
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;
    }
}

What parameter or object do I need to access in order to save the excel file as page width instead of page height?

like image 839
bl4kh4k Avatar asked Apr 22 '12 21:04

bl4kh4k


People also ask

What is ExportAsFixedFormat?

The ExportAsFixedFormat method is used to publish a workbook to either the PDF or XPS format.

How do I save an Excel macro as a PDF?

Before you run the macro, select the sheet(s) that you want to export to the PDF file. When the macro starts, it sets variables for the active sheet, and the active workbook. Those will be used to set the default file name and folder. A time stamp will be added to the default name, in the format yyyymmdd_hhmm.


2 Answers

I have found the property required to force the export of your workbook in a PDF with Landscape view.

try 
{ 
    ((Microsoft.Office.Interop.Excel._Worksheet)  
    excelWorkbook.ActiveSheet).PageSetup.Orientation =  
    Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;

    excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,  
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
        "\\" + output); 
} 
like image 169
Steve Avatar answered Oct 01 '22 12:10

Steve


  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.

like image 36
Siva.Net Avatar answered Oct 01 '22 11:10

Siva.Net