DEV Community

Cover image for Converting Excel XLSX to PDF with C# .NET
Janki Mehta
Janki Mehta

Posted on

Converting Excel XLSX to PDF with C# .NET

Converting Excel spreadsheets to PDF is a common task many businesses and developers need to do. Whether you want to create a PDF report from Excel data or let users download Excel charts and sheets in PDF format, C# and the .NET Framework make Excel to PDF conversion straightforward.

In this post, I'll walk through the steps to programmatically convert Excel XLSX files to PDF using C# and .NET. We'll cover:

  • Setting up the project
  • Adding the necessary NuGet packages
  • Loading the Excel file
  • Converting the XLSX to PDF
  • Saving the PDF file

Setting Up the C# Project

First, create a new C# .NET Framework console application in Visual Studio.

To work with Excel files, we need to add a reference to the Microsoft Excel Object Library. In Solution Explorer, right-click on References and choose Add Reference. Check the box for Microsoft.Office.Interop.Excel and click OK. This allows us to leverage Excel objects like Workbooks and Worksheets in our C# code.

Next, we need to install the NuGet packages that will do the bulk of the PDF conversion work. Search for and install the following packages:

  • 1. Spire.XLS
  • 2. Spire.PDF

Spire.XLS allows us to easily load and read Excel files, while Spire.PDF provides methods to convert .NET objects to PDF programmatically.

Loading the Excel XLSX File

With our references set up, let's add code to load the source XLSX file.

First, add a statement to import the Spire.XLS namespace:

using Spire.Xls;
Enter fullscreen mode Exit fullscreen mode

Next, we'll create a method that accepts the filename of the Excel file and loads it into a Workbook object:

private static Workbook LoadExcel(string filename)
  Workbook workbook = new Workbook();

  return workbook;
Enter fullscreen mode Exit fullscreen mode

This opens the Excel file specified by the filename parameter and loads it into a Workbook object that we can then access sheets from.

Converting the Excel File to PDF

Once our XLSX is loaded, we can convert the Excel data to a PDF file.

Add a using statement to import Spire.PDF:

using Spire.Pdf;
Enter fullscreen mode Exit fullscreen mode

Then we'll create a method to convert the Workbook to a PDF and save it:

private static void ConvertToPDF(Workbook workbook, string pdfFilename)
  PdfDocument pdf = new PdfDocument();

  // Loop through each worksheet and convert to PDF
  foreach (Worksheet sheet in workbook.Worksheets)
    // Add a new section for this sheet
    PdfSection section = pdf.Sections.Add();

    // Convert worksheet to image
    Image image = sheet.ToImage();

    // Add image to PDF section

  // Save PDF document
Enter fullscreen mode Exit fullscreen mode

This iterates through each Worksheet in the Workbook. For every sheet, it converts the sheet to an Image and adds it to a new section in the PDF document. Once all sheets are processed, it saves the full PDF to the file path specified.

Putting It All Together

Finally, let's call the methods we created to load an Excel file, convert it to a PDF, and save the PDF:

static void Main(string[] args)
  string inputFile = @"C:\Users\Example\Documents\report.xlsx";
  string outputFile = @"C:\Users\Example\Documents\report.pdf";

  Workbook workbook = LoadExcel(inputFile);
  ConvertToPDF(workbook, outputFile);

  Console.WriteLine("Excel converted to PDF!");
Enter fullscreen mode Exit fullscreen mode

And that's it! When run, this will take the example report.xlsx file and generate a PDF version named report.pdf in the Documents folder.

The key steps are:

  • Referencing the Excel and PDF libraries
  • Loading the XLSX file into a Workbook
  • Looping through each Worksheet
  • Converting the Worksheet to an Image
  • Adding the Image to a PDF section
  • Saving the full PDFDocument

This basic demo shows how straightforward it is to programmatically convert Excel XLSX to PDF with C# and .NET. From here you can enhance it to add options like custom paper size, image compression, and other features.

Top comments (0)