How to convert Excel XLSX to PDF using C# in .NET Framework

Excel files are inherently flexible, but sometimes converting to PDF format simply offers better compatibility. Rather than covering the arduous task of setting up this conversion process, we will instead be looking at how to apply a Cloudmersive API to do it instead.

Let’s start by running this command in the Package Manager console to install our API client:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

Now we will use the following code snippet to call ConvertDocumentAutodetectToPdf. Note that this function can be used with many different document and image formats, so you may wish to check our documentation section for a full list.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDocumentAutodetectToPdfExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.
try
{
// Convert Document to PDF
byte[] result = apiInstance.ConvertDocumentAutodetectToPdf(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentAutodetectToPdf: " + e.Message );
}
}
}
}

Ok, we are done. Easy, right? Our conversion API also includes other functions that allow you to alter documents, convert PDFs to PNG arrays, and apply HTML templates.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store