Excel to PDF – Convert Excel to PDF in C#

Excel to PDF

The spreadsheet software is commonly used to create budgets, produce graphs and charts, and for storing and sort data. Also, within businesses, spreadsheet software is used to forecast future performance, calculate tax, complete basic payroll, produce charts, and calculate revenues. Excel workbooks are the default output of such applications and you may need to share such worksheets across the organizations. So in order to view such files, you need to use one of these applications (Microsoft Excel, Lotus 1-2-3, Microsoft Works Spreadsheet, Open Office Calc, Google Drive Spreadsheet, etc.). Also, different versions of these applications may render the files in different formatting. Therefore, a viable solution is to convert Excel files to PDF format as the formatting/layout will preserve on all platforms.

Excel Processing API

Aspose.Cells Cloud is a REST-based API providing the capabilities to create, edit and render Excel files to PDF and other supported file formats. Create Desktop, web & cloud apps to provide Excel & OpenOffice spreadsheets processing functionality. Other features may include access and modifying workbooks, worksheets, spreadsheets, rows as well as individual cells, without installing any third-party software. Now in order to make things further simpler, we have created programming language-specific SDKs which are basically a wrapper around the Cloud API. So in this article, we are going to discuss the usage of Aspose.Cells Cloud SDK for .NET for Excel conversion to PDF using C# .NET. To get started, the first step is to install the Cloud SDK on the system. For more information, please visit How to install Aspose.Cloud SDKs.

Convert Excel to PDF in C#

Please follow the instructions given below to convert the Excel files available in Cloud storage to PDF format. As a result of this operation, all the worksheets within the source workbook will be converted to PDF format.

  • First of all, create an instance of CellsApi class while passing ClientID and ClientSecret details as arguments
  • Secondly, read the input XLSX file into stream object using File.OpenRead
  • Now call the CellsWorkbookPutConvertWorkbook(..) method taking fileStream holding input excel, ouptut format details and resultant PDF name as arguments

The same excel file and resultant PDF generated in the above example can be downloaded from Book1.xlsx and Converted.pdf.

Image 1:- Excel to PDF conversion preview.

In can you only need to convert a particular worksheet, please try using the CellsWorksheetsGetWorksheet(…) method of CelslApi class.

Excel to PDF using cURL Command

The REST APIs can easily be accessed via cURL commands from the terminal/command shell. Since Aspose.Cells Cloud is developed as per REST architecture, so it can also be accessed via cURL commands. But before we proceed towards conversion operations, please note that our APIs are only accessible to authorized persons. So we need to generate JWT access token-based client credentials specified over Aspose.Cloud dashboard. For further details, please visit How to Obtain JWT token using a Client ID and Client Secret key.

Please execute the following command to generate a JWT access token:

curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=4db2f826-bf9c-42e7-8b2a-8cbca2d15553&client_secret=d757548a9f2558c39c2feebdf85b4c44" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Once we have a JWT token, please execute the following command to perform the conversion of Excel file in request content to PDF format and save the output in Cloud storage.

curl -X PUT "https://api.aspose.cloud/v3.0/cells/convert?format=PDF&outPath=converted.pdf" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"File":{}}

Conclusion

We have discussed the conversion of Excel files to PDF format using REST API. We have also discussed that we can either convert a complete workbook or render a specific Worksheet to PDF format using the C# .NET code snippet or using the cURL command. We have also learned about the Cloud SDKs developed on top of REST APIs and since the SDKs are developed as per MIT license, so the complete source code is available over GitHub.

Nevertheless, if you encounter any issues or you have any further queries, please feel free to contact us through the free customer support forum.

Related articles

We also recommend visiting the following links to learn about