Friday, 08 July 2011 05:23

Convert Excel to PDF-Excel to PDF in C#,VB.NET

Written by  support iceblue
Rate this item
(0 votes)

In order to convert Excel to PDF in C#/VB.NET, you may have searched a lot, finding huge lines of codes but not perfect in quality. Here is the nice solution you are looking for. Within this section, we will demonstrate how to convert Excel to PDF using C#/VB.NET. This conversion will be successfully managed within just several lines of codes, but the quality will be absolutely perfect; As the PDF will be the same as your Excel file.

Spire.XLS for .NET is .NET Excel component which enables your .NET applications to fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. It also fully supports converting files from Excel to PDF, Excel to HTML, Excel to CSV, Excel to Text, Excel to Image and Excel to XML. Any kind of trial and evaluation is always welcomed. Please feel free to download Spire.Xls for .NET to have a trial.

Spire.XLS now offers a new method to convert excel to PDF, which supports convert chart, shape and smart art in excel file to PDF. Please refer to new method of convert Excel to PDF.

The following is the code samples for converting Excel to PDF in C#/VB.NET. You can directly copy and paste into your project to have a try and convert your Excel to PDF for personal use.

C#.NET code for converting Excel to PDF

[C#]
using Spire.Xls;
using Spire.Pdf;
using Spire.Xls.Converter;

namespace ExceltoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
           // load Excel file
           Workbook workbook = new Workbook();
           workbook.LoadFromFile("D:\\test.xlsx");
 
           // Set PDF template
           PdfDocument pdfDocument = new PdfDocument();
           pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
           pdfDocument.PageSettings.Width = 970;
           pdfDocument.PageSettings.Height = 850;

           //Convert Excel to PDF using the template above
           PdfConverter pdfConverter = new PdfConverter(workbook);
           PdfConverterSettings settings = new PdfConverterSettings();
           settings.TemplateDocument = pdfDocument;
           pdfDocument = pdfConverter.Convert(settings);

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

VB.NET code for converting Excel to PDF

[VB.NET]
Imports Spire.Xls
Imports Spire.Pdf
Imports Spire.Xls.Converter

Namespace ExceltoPDF
	Class Program
		Private Shared Sub Main(args As String())
			' 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")
		End Sub
	End Class
End Namespace
          

After this code, you can run this application and you will see the PDF converted from Excel. Below is a comparison of the original Excel and target PDF, please have a check.

 

Excel to PDF

 

More about this .NET Excel Library

Additional Info

  • tutorial_title: Convert Excel to PDF
Last modified on Wednesday, 19 February 2014 08:30