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

Background: Convert Excel to PDF with C#/VB.NET

I have an Excel file and I’d like to convert its spreadsheet to PDF programmatically within C# or VB.NET.

Solution: Spire.XLS for .NET

Spire.XLS for .NET is a standalone .NET excel component which enables your .NET applications fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. And Spire.XLS for .NET allows user to use C#/VB.NET convert Excel to PDF. More about Spire.XLS for .NET…

Guide: How to Use C#/VB.NET Convert Excel to PDF

Download and install Spire.XLS for .NET. Use the code below to convert Excel to PDF.

Note: Use alias here

Convert Excel to PDF with C#
[C#]

extern alias SpirePdf;
using Spire.Xls;
using Spire.Xls.Converter;
using Spire.Pdf;
using pdf = SpirePdf.Spire.Pdf;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("test1.xls");
            PdfConverter pdfConverter = new PdfConverter(workbook);

            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape;
            pdfDocument.PageSettings.Width = 970;
            pdfDocument.PageSettings.Height = 850;

            PdfConverterSettings settings = new PdfConverterSettings();
            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);

            pdfDocument.SaveToFile("test.pdf");
        }

    }

}

Convert Excel to PDF with VB.NET
[VB.NET]

extern alias SpirePdf;
Imports Spire.Xls
Imports Spire.Xls.Converter
Imports Spire.Pdf
Imports pdf = SpirePdf.Spire.Pdf

Namespace ConsoleTest
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("test1.xls")
			Dim pdfConverter As New PdfConverter(workbook)

			Dim pdfDocument As New PdfDocument()
			pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape
			pdfDocument.PageSettings.Width = 970
			pdfDocument.PageSettings.Height = 850

			Dim settings As New PdfConverterSettings()
			settings.TemplateDocument = pdfDocument
			pdfDocument = pdfConverter.Convert(settings)

			pdfDocument.SaveToFile("test.pdf")
		End Sub

	End Class

 

Related Products:

Spire.Doc for .NET
Spire.PDF for .NET
Spire.DataExport for .NET
Spire.Office for .NET
Free PDF Converter

Leave a Reply