How to Convert Excel to PDF with C#/VB.NET

As is known, Excel is very powerful on managing data information. Users can save necessary information in Excel file and then calculate data. Also, users can set format for cells to have a more beautiful appearance. And a well formatted Excel file can make readers understand data more clearly.

However, sometimes, users would like to convert Excel to PDF document because PDF are more convenient for sending and printing on different operation systems. Also, PDF can give readers a better reading effect.

In this post, I want to introduce a method about how to convert Excel to PDF with C#/VB.NET. And I use one .NET Excel component, Spire.XLS for .NET in the method, so its dll file has been added as reference in project.

STEPS

Before coding, the following namespace should be used.

C#
using System;
using Spire.Xls;
using Spire.Xls.Converter;
using Spire.Pdf;

VB.NET

Imports System
Imports Spire.Xls
Imports Spire.Xls.Converter
Imports Spire.Pdf

Step 1. Load Excel

Load the Excel file which I want to convert to PDF from computer by using workbook.LoadFromFile() method.

C#

            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@”E:\work\Documents\student info.xlsx”);

VB.NET

            Dim workbook As New Workbook()
            workbook.LoadFromFile(“E:\work\Documents\student info.xlsx”)

Step 2. Set PDF Document

Create a new PDF document and then set page, including page orientation, width and height. Then, take this created PDF document as template when converting.

C#

            PdfDocument PDFdocument = new PdfDocument();
            PDFdocument.PageSettings.Orientation = PdfPageOrientation.Portrait;
            PDFdocument.PageSettings.Width = 850;
            PDFdocument.PageSettings.Height = 970;
            PdfConverterSettings settings = new PdfConverterSettings();
            settings.TemplateDocument = PDFdocument;

VB.NET

            Dim PDFdocument As New PdfDocument()
            PDFdocument.PageSettings.Orientation = PdfPageOrientation.Portrait
            PDFdocument.PageSettings.Width = 850
            PDFdocument.PageSettings.Height = 970
            Dim settings As New PdfConverterSettings()
            settings.TemplateDocument = PDFdocument

Step 3. Convert

Convert Excel file to PDF by declaring a PDFConverter and then assign value as pdfconverter.Convert(settings) for PDFdocument.

C#

            PdfConverter pdfconverter = new PdfConverter(workbook);
            PDFdocument = pdfconverter.Convert(settings);

VB.NET

            Dim pdfconverter As New PdfConverter(workbook)
            PDFdocument = pdfconverter.Convert(settings)

Step 4. Save and Launch

Save the converted document by using PDFdocument.SaveToFile() method. Then, launch this document.

C#

            PDFdocument.SaveToFile(“toPDF.pdf”);
            System.Diagnostics.Process.Start(“toPDF.pdf”);

VB.NET

            PDFdocument.SaveToFile(“toPDF.pdf”)
            System.Diagnostics.Process.Start(“toPDF.pdf”)

RESULT

Freely Download Spire.XLS for .NET

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s