excel -->PDF

 

下载并安装SaveAsPDF.exe

This section illustrates how to save an Excel 2007 workbook programmatically in the PDF and XPS file formats. There are five steps to this procedure:

  1. Adding a reference to the Excel 12.0 Object Library.

  2. Importing the Excel Interop assembly namespace.

  3. Creating an instance of the ApplicationClass object.

  4. Declaring the appropriate variables.

  5. Implementing the conversion code.

1. Adding a Reference to the Excel 12.0 Object Library

Begin by adding a reference to the Microsoft Excel 12.0 Object Library to the Visual Studio project.

To add a reference to the Excel 12.0 Object Library

  1. Right-click the project in the Visual Studio Solution Explorer.

  2. Select Add Reference….

  3. Select the COM tab in the Add Reference dialog box.

  4. Scroll down to the Microsoft Excel 12.0 Object Library component and select it.

  5. Click OK to add the reference.



Figure 1. Adding a Reference

Add Reference Dialog Box

2. Importing the Microsoft.Office.Interop.Excel Namespace

The next step is to import the Microsoft.Office.Interop.Excel namespace to the project.

To import the Microsoft.Office.Interop.Excel Assembly Namespace

  1. For Visual Basic projects, right-click the project in the Visual Studio Solution Explorer and select the Properties menu item.

  2. On the Visual Basic Properties Page Dialog Box, select the References tab.

  3. Select the check box control next to the Microsoft.Office.Interop.Excel entry in the list of imported namespaces.

  4. Close the Visual Basic Properties Page dialog box.



Figure 2. Import Namespace in Visual Basic

Importing Namespace

For Microsoft Visual C# projects add the following line to the top of the source file:

using Microsoft.Office.Interop.Excel;

3. Creating an Instance of the ApplicationClass Object

To work with the Excel object model create an instance of the top-level ApplicationClass object and declare a variable to hold the reference to the workbook.

ApplicationClass excelApplication = new ApplicationClass();
Workbook excelWorkBook = null;

4. Declaring the Appropriate Variables

You must declare variables for parameters that are passed to methods used in the conversion code. This modification makes the conversion code easier to read.

The variables are used with the Workbooks.Open method.

The paramSourceBookPath variable specifies the path and filename of the Excel workbook to export as PDF or XPS.

You can use the paramMissing variable to call methods that accept optional parameters. Optional parameters are only optional when using Microsoft Visual Basic. You must specify a value for optional parameters when using C#. Using Type.Missing as the value for an optional parameter signals to the method being called that the parameter is not being specified and that the method should use the parameter's default value.

string paramSourceBookPath = @"C:\Temp\Test.xlsx";
object paramMissing = Type.Missing;

Use the following variables with the Workbook.ExportAsFixedFormat method. The paramExportFormat variable is important because it specifies the exported workbook's format. The paramExportFormat variable is of type XlFixedFormatType, an enumerated type that has two values, xlTypePDF and xlTypeXPS. The sample code shown sets the paramExportFormat variable to the XlFixedFormatType.xlTypePDF value to export a workbook to the PDF format.

To change the code to export a workbook in the XPS format the variable must be set to the XlFixedFormatType.xlTypeXPSvalue. For more information about the ExportAsFixedFormat method and the parameters it accepts, seeWorkbook.ExportAsFixedFormat Method.

string paramExportFilePath = @"C:\Temp\Test.pdf";
XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
XlFixedFormatQuality paramExportQuality = 
    XlFixedFormatQuality.xlQualityStandard;
bool paramOpenAfterPublish = false;
bool paramIncludeDocProps = true;
bool paramIgnorePrintAreas = true;
object paramFromPage = Type.Missing;
object paramToPage = Type.Missing;

5. Implementing the Conversion Code

The final step is to implement the conversion code.

To implement the conversion code

  1. Add code to open the source workbook.

  2. Export it to the specified format.

  3. Quit Excel.

It is critical to have the Save as PDF add-in installed. If it is not, the call to the Workbook.ExportAsFixedFormat method generates an exception. To handle the exception, the conversion code is wrapped in a Try…Catch block. Code located in a finally block does two things: closes the Excel workbook and application objects, and releases references to the underlying Excel COM objects, allowing Excel to unload from memory. For more information about releasing COM objects when using managed code see Chapter 2: Basics of Office Interoperability (Part 2 of 3) from the book Microsoft .NET Development for Microsoft Office.

try
{
    // Open the source workbook.
    excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
        paramMissing, paramMissing, paramMissing, paramMissing,
        paramMissing, paramMissing, paramMissing, paramMissing, 
        paramMissing, paramMissing, paramMissing, paramMissing, 
        paramMissing, paramMissing);

    // Save it in the target format.
    if (excelWorkBook != null)
        excelWorkBook.ExportAsFixedFormat(paramExportFormat,
            paramExportFilePath, paramExportQuality,
            paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, 
            paramToPage, paramOpenAfterPublish, 
            paramMissing);
}
catch (Exception ex)
{
    // Respond to the error.
}
finally
{
    // Close the workbook object.
    if (excelWorkBook != null)
    {
        excelWorkBook.Close(false, paramMissing, paramMissing);
        excelWorkBook = null;
    }

    // Quit Excel and release the ApplicationClass object.
    if (excelApplication != null)
    {
        excelApplication.Quit();
        excelApplication = null;
    }

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

 

posted @ 2011-01-26 15:09  Nina  阅读(698)  评论(0编辑  收藏  举报