Skip to main content Skip to footer

Programmatically Convert Excel XLSX to PDF Using C# .NET

If you have Excel spreadsheets generated from .NET Core applications, you may find it helpful to store your files in a PDF format.

Why do I need to convert an Excel spreadsheet to PDF?

Several reasons include:

  • Storing Excel spreadsheets for long-term preservation.
  • Not having MS Office installed on a system, but still wanting to open, print, or distribute your Excel spreadsheets.
  • Consistency of presentation of Excel spreadsheets when opened on different systems.

Alternatively, you may need to create and distribute the following company reports:

  • Profit & Loss Statement
  • Company Budget
  • Sales Forecast
  • Income Statement

Document Solutions for Excel (DsExcel) is a high-speed, small-footprint spreadsheet API that requires no dependencies on Excel. With full .NET Core support, you can generate, load, modify, and convert spreadsheets in .NET Framework, .NET Core, Mono, and Xamarin. Apps using this spreadsheet API can be deployed to the cloud, Windows, Mac, or Linux.

Ready to Try it Out? Download Document Solutions Today!

Steps for Programmatically Converting an Excel Spreadsheet to a PDF in C#/.NET

  1. Create an Excel Spreadsheet using the .NET Excel API

  2. Convert the Excel Spreadsheet to PDF Using the Save Method

  3. Load an Existing Excel Spreadsheet and to Convert to PDF

Note: When working with DsExcel on MAC or Linux, specify the Workbook.FontFolderPath and place the necessary fonts in it before exporting spreadsheets to PDF.

For more information, visit: Configure Fonts and Set Style.

Step 1: Create an Excel Spreadsheet using the .NET Excel API

Follow this link to create a basic Excel spreadsheet on Windows, MAC, or Linux.

At the end of this tutorial, the spreadsheet will look like this:

Create Spreadsheet

Step 2: Convert the Excel Spreadsheet to PDF Using the Save Method

In Step 1, we created an Excel spreadsheet using the DsExcel workbook and saved it to an Excel file. Instead of saving to Excel, you can directly save the workbook to PDF. Please note if you need to print the worksheet on a single PDF, you can set additional options through the PageSetup class of the worksheet.

worksheet.PageSetup.Orientation = PageOrientation.Landscape;  
worksheet.PageSetup.IsPercentScale = false;  
worksheet.PageSetup.FitToPagesWide = 1;  
worksheet.PageSetup.FitToPagesTall = 1;  
workbook.Save(@"SimpleBudget.pdf");

The PDF will look like this:

Convert to PDF

Step 3: Load an Existing Excel Spreadsheet and to Convert to PDF

If you want to convert any of your Excel files (whether created with Excel, DsExcel, or from another third-party tool), you can do so in just three steps using DsExcel.

Suppose you want to convert an Excel file that calculates the total account data summary (per different age groups in a region) to a PDF.

Load Spreadsheet

Follow these steps:

1. Create an empty workbook.

var workbook = new GrapeCity.Documents.Excel.Workbook();

2. Load the Excel file into the workbook.

workbook.Open("AgingReport.xlsx");

3. Add additional options through the PageSetup class of the worksheet. This will export the whole worksheet to a single page of PDF.

workbook.Worksheets[0].PageSetup.Orientation = PageOrientation.Landscape;
workbook.Worksheets[0].PageSetup.IsPercentScale = false;
workbook.Worksheets[0].PageSetup.FitToPagesWide = 1;
workbook.Worksheets[0].PageSetup.FitToPagesTall = 1;

4. Save To PDF.

workbook.Save("AgingReport.pdf");

You have easily converted your Excel spreadsheet to PDF:

Convert to PDF

Supported PDF export features include:

Try the demo here.

Visit this help topic for more info on how to convert Excel to PDF in DsExcel Java.

Ready to Try it Out? Download Document Solutions Today!

Please leave a comment below if this feature satisfies your requirement or if you are looking for some additional features!

comments powered by Disqus