Programmatically Convert Excel XLSX to PDF Using C# .NET
Tags: webdev, devops, csharp, tutorial
Published: 2023-10-30 16:14:38

---
canonical_url: https://developer.mescius.com/blogs/programmatically-convert-excel-xlsx-to-pdf-using-c-net
description: Learn how to programmatically convert Excel XLSX spreadsheets to PDF documents using C# .NET. See more and build your application today.
---
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

[**GrapeCity Documents for Excel**](https://developer.mescius.com/document-solutions/dot-net-excel-api) (**GcExcel**) 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**.

{% embed https://youtu.be/lkd5JvTTLCw %}

## 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 **GcExcel** 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](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ConfigureFontsandSetStyle.html).

#### <a id="create"></a>Step 1: Create an Excel Spreadsheet using the .NET Excel API

[Follow this link](https://developer.mescius.com/blogs/get-started-with-document-api-excel-windows-mac-linux) to create a basic Excel spreadsheet on **Windows**, **MAC**, or **Linux**.

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

![convert](https://global-cdn.grapecity.com/blogs/gcdocuments/20220310-how-to-convert-excel-xlsx-spreadsheets-to-pdf-using-c-sharp-net-6/Image1_CreateSpreadsheet.png)

#### <a id="convert"></a>Step 2: Convert the Excel Spreadsheet to PDF Using the Save Method

In Step 1, we created an Excel spreadsheet using the **GcExcel** 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](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/GrapeCity.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup_members.html) 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](https://global-cdn.grapecity.com/blogs/gcdocuments/20220310-how-to-convert-excel-xlsx-spreadsheets-to-pdf-using-c-sharp-net-6/Image2_ConverttoPDF.png)

#### <a id="load"></a>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, **GcExcel,** or from another third-party tool), you can do so in just three steps using **GcExcel**.

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**.

![convert](https://global-cdn.grapecity.com/blogs/gcdocuments/20220310-how-to-convert-excel-xlsx-spreadsheets-to-pdf-using-c-sharp-net-6/Image3_Loadspreadsheet.png)

Follow these steps:

**1\. Create an empty workbook.**`<span class="hljs-keyword"></span>`

```
    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](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/GrapeCity.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup_members.html "/document-solutions/dot-net-excel-api/docs/online/GrapeCity.Documents.Excel~GrapeCity.Documents.Excel.IPageSetup_members.html") 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](https://global-cdn.grapecity.com/blogs/gcdocuments/20220310-how-to-convert-excel-xlsx-spreadsheets-to-pdf-using-c-sharp-net-6/Image4_converttoPDF.png)

**Supported PDF export features include:**

*   [Export Chart](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/exportchart)
*   [Export Shape](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/exportshape)
*   [PDF Export progress](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/pageprintevents)
*   [Page Setup](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/pdfpagesetup)
*   [PrintManager](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/printmanager)
*   [Export text](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/text)
*   [Center across selection](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/centeracrossselection)
*   [Export outline column](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/outlinecolumntopdf)
*   [Print transparent cell](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/printtransparentcell)
*   [Save background pictures to pdf](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savebackgroundpicturestopdf)
*   [Export borders](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveborder)
*   [Export Conditional Formatting](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveconditionalformatting)
*   [Export picture](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savepicturetopdf)
*   [Export Pivot Table](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savepivottable)
*   [Export fills](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saverangefilltopdf)
*   [Export sheet background image](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savesheetbackgroundtopdf)
*   [Export slicers](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveslicer)
*   [Export sparklines](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savesparklinestopdf)
*   [Export table](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/savetable)
*   [Save workbook to PDF](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveworkbooktopdf)
*   [Save worksheet to PDF](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/saveworksheettopdf)
*   [Set document properties for PDF document](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/setdocumentpropertiestopdf)
*   [Set font's folder](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/setfontsfolderpath)
*   [Set export image quality](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/setimagequality)
*   [Set PickTrayByPDFSize option for PDF document](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/setpicktraybypdfsizeoptiontopdf)
*   [Set security options for PDF document](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/setsecurityoptionstopdf)
*   [Shrink to fit for wrapped text](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/shrinktofitforwrappedtext)

Try the demo [here](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos/GrapeCity.Documents.Excel.Examples.Features.PDFExporting).

Visit this [help topic](https://developer.mescius.com/document-solutions/java-excel-api/docs/online/ExporttoaPDFFile.html) for more info on how to convert Excel to **PDF** in **GcExcel Java**.

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