Ross from Brooklin
Ross from Brooklin

Reputation: 303

Save Excel spreadsheet as PDF

I'm trying to save an Excel spreadsheet as a PDF file using Visual Basic. I've found some sample code online (see below) but it has me open a Workbook object which Visual Basic doesn't seem to recognize any more. Suggestions...

                       ' load Excel file
        Dim workbook As New Workbook()
        workbook.LoadFromFile("D:\test.xlsx")

        ' Set PDF template
        Dim pdfDocument As New PdfDocument()
        pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
        pdfDocument.PageSettings.Width = 970
        pdfDocument.PageSettings.Height = 850

        'Convert Excel to PDF using the template above
        Dim pdfConverter As New PdfConverter(workbook)
        Dim settings As New PdfConverterSettings()
        settings.TemplateDocument = pdfDocument
        pdfDocument = pdfConverter.Convert(settings)

        ' Save and preview PDF
        pdfDocument.SaveToFile("sample.pdf")
        System.Diagnostics.Process.Start("sample.pdf")

Upvotes: 2

Views: 22961

Answers (4)

jacky
jacky

Reputation: 1

The Workbook is not a class of Excel Interop nor a class of System. This sample code is based on Spire.XLS, a 3rd party component which enables users to manipulate Excel file in .NET. Before you can run the program, you need to download and reference the DLL to your project.

Upvotes: 0

T.S.
T.S.

Reputation: 19330

This is just one of the ways of how you can create PDF out of Excel or for that matter - any program that you can access programmatically (to broaden your experience). It is by using virtual printer. You will need

  • Download virtual PDF printer (many are available and some are free). Also, understand what type of PDF any given printer creates - raster pdf or vector pdf. Raster creates much larger files.
  • Install it (virtual printer)
  • In code - you usually setup the printer properties and then
  • you, in code, open excel using interop and call Print, in which you set the printer - your virtual PDF printer and depending on the printer, you set option "Print To File". Some virtual printers don't require that because you preset their properties or even (some printers) registry keys, where you set the file name.

Code examples are usually available from printer vendors. And there are more on different related blogs. Google "virtual printer for .net"

And what I think, if you using older interop/excel (sorry, I can't really tell the version cut off) - this is only way to do it.

Upvotes: 0

SSS
SSS

Reputation: 5393

Late-bound version of nutsch's answer..

Option Strict Off 'Required for Late Binding     

Module XL
  Sub ExcelPDF()
    Dim xl As Object
    xl = CreateObject("Excel.Application")
    Dim xwb As Object = xl.Workbooks.Open("D:\test.xlsx")
    xwb.ActiveSheet.ExportAsFixedFormat(0, "D:\sample.pdf")
    xl.Quit()
  End Sub
End Module

P.S. I recommend developing with the Office PIA's (so you get Intellisense & help) and then switching to late-binding before publishing, so you aren't locked into a specific version of Office (also, so you don't need to distribute the PIA's with your app)

Upvotes: 2

nutsch
nutsch

Reputation: 5962

You can go simpler by using the .ExportAsFixedFormat function, e.g.

Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")

workbook.activesheet.ExportAsFixedFormat xlTypePDF, "D:\test.pdf"

Upvotes: 2

Related Questions