How Convert Excel XLS or XLSX to PDF in C#

Andrew Wilson
2 min readDec 22, 2023

--

Excel is a powerful tool for organizing and analyzing data, but it can be a bit of a headache when it comes to sharing data with others, as Excel files are easily edited, which can lead to errors or changes that you didn’t intend. Given this situation, converting Excel to PDF comes in handy. Below is a guide on how to convert an Excel (XLS or XLSX) document to PDF in C# using a free third-party library.

.NET Library for Converting Excel to PDF

To get started with the third-party .NET library — Free Spire.XLS for .NET, you’ll need to download it from this link to manully reference the DLLs or install directly via NuGet.

Convert Excel (XLS or XLSX) to PDF in C#

Main steps:

  1. Load a .xls or .xlsx file;
  2. Set the Excel to PDF conversion options through the properties under the ConverterSetting class;
  3. Save the XLS or XLSX file to PDF format.

Sample code:

using Spire.Xls;

namespace Excel2PDF
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();

//Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.xlsx");

//Set worksheets to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = true;

//Save to PDF
workbook.SaveToFile("Excel2Pdf.pdf", FileFormat.PDF);
}
}
}

Result:

Convert Excel to PDF

As we can see above, through the Workbook.SaveToFile() method, each worksheet in the Excel file is converted into a separate page in the resulting PDF file. If you only need to convert a specific worksheet, Free Spire.XLS for .NET library also offers the Worksheet.SaveToPdf() method.

Refer to: Convert a Specific Worksheet to PDF in C#

--

--

Andrew Wilson
Andrew Wilson

Written by Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.

No responses yet