Excel (XLS, XLSX) and PDF files are among those document formats that are widely used in almost every business. For such commonly used files, there are many scenarios in which we need to convert one file into another format. In this article, we will learn different ways for how to convert Excel spreadsheets into PDF format using C# with .NET applications.
- .NET API for Excel Files to PDF Conversion
- Excel Sheets to PDF Conversion
- Sequence of Excel Sheets to PDF Conversion
- Specific List of Excel Sheets to PDF Conversion
- Convert Selected Cell Range from Excel Sheet to PDF
.NET API for Excel Files to PDF Conversion
GroupDocs.Conversion provides APIs that allow the Excel files converted to PDF format within .NET applications. In this article, we will use GroupDocs.Conversion for .NET to convert the Excel XLS/XLSX files data into PDF format. Additionally, the API supports the conversion of many other file formats like word-processing documents, spreadsheets, presentations, eBooks, images, etc that are mentioned in the documentation.
You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.
PM> Install-Package GroupDocs.Conversion
Convert Excel Sheets to PDF - C#
The following steps convert the complete workbook (all sheets) to PDF format using C#.
- Prepare the loading options using the SpreadsheetLoadOptions.
- Load the Excel spreadsheet using Converter.
- Call the Convert() method using PdfConvertOptions to convert all the sheets and save to PDF format.
The following is the C# source code for how to convert the complete Excel workbook to PDF within the .NET application.
Sequence of Excel Sheets Conversion to PDF - C#
It is not always needed to transform the complete workbook. We can also convert any consecutive number of sheets. The following are the steps to convert any sub-sequence of the Excel workbook sheet(s) into PDF format using C#.
- Load the Excel file using the Converter.
- Define the conversion options using PdfConvertOptions.
- Set the starting sheet number and number of further sheets in sequence.
- Call the Convert() method with the conversion options to get the subset of sheets in sequence saved in PDF format.
The following is the C# source code that converts the sheets in sequence, i.e. sheet numbers 2,3, and 4 to PDF within the .NET application.
Specific Excel Sheets to PDF Conversion - C#
We can simply provide the list of the sheet numbers for the conversion of specific sheets. The following are the steps for how to convert any specific list of sheet numbers into PDF format using C#.
- Load the spreadsheet file using the Converter.
- Select the sheets numbers and set as list using PdfConvertOptions.
- Call the Convert() method with the conversion options to convert the listed sheets into PDF format.
The following C# code snippet converts sheet numbers 1, 3, and 5 to PDF within the .NET application.
Convert the Selected Cell Range of Excel Sheet to PDF - C#
Last but not least, in fact, the most tricky one, we can also convert any cell range of Excel sheet(s) in almost a similar way as other approaches. The following are the steps to convert any cell range of workbook sheet(s) into PDF format using C#.
- Firstly, define the cell range for conversion using the SpreadsheetLoadOptions.
- Load the spreadsheet file using the Converter.
- Select the sheets either by exact sheet numbers or sub-sequence using PdfConvertOptions.
- Call the Convert() method with the conversion options to convert the selected cell range of selected sheets into PDF format.
The following code converts the cell range (A1:C20) of sheet numbers 2, 3, and 4 to PDF format using C#.
Conclusion
To conclude, we learned different ways to convert Excel spreadsheets into PDF format using C#. Firstly, we looked to convert the complete workbook into PDF format, then we converted the sub-sequence of sheets. Later, we learned how to convert any sheet(s) by providing the list of exact sheet numbers, and lastly, we obtained the PDF file from the selected cell range of selected sheet(s).
Learn more about GroupDocs.Conversion APIs from the documentation. For queries, contact us via the forum.