C#/VB.NET - How to Convert Excel (XLS/XLSX) to PDF

Alexander Stock
4 min readNov 15, 2022

--

Excel is one of the most widely used programs for creating and managing spreadsheets. However, when it comes to sharing these files with others, Excel may not deliver the best results. This is because the formatting features of Excel are not always compatible with other devices and software. One way to get around this issue is by converting spreadsheets into PDFs. In this article, I am going to introduce how to convert a workbook or a worksheet to PDF in C# and VB.NET by using Spire.XLS for .NET library.

  • Convert an Excel Workbook to PDF in C# and VB.NET
  • Convert a Specified Worksheet to PDF in C# and VB.NET

Add Spire.Xls.dll as Reference

Method #1: Download Spire.XLS and unzip the package somewhere on your disk to find the “BIN” folder. Spire.XLS has the DLLs compiled for multiple versions of .NET Framework as well as for .NET Core and other platforms. Choose the DLLs from the folder that you exactly need and add them all as dependencies in your project.

Method #2: Create a .NET application in you Visual Studio, and install Spire.XLS directly through NuGet. NuGet package manager will automatically install the correct version that fits in with your application.

Convert an Excel Workbook to PDF in C# and VB.NET

If an Excel workbook consists of several worksheets and the data of each sheet is highly related, it would be ideal to convert an entire Excel file to a PDF file. The following are the steps to convert an Excel workbook to PDF using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
  • Set Excel page margins through TopMargin, BottomMargin, LeftMargin and RightMargin properties under PageSetup object.
  • Save the workbook to PDF using Workbook.SaveToFile() method.

[C#]

using Spire.Xls;

namespace ConvertWorkbookToPdf
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();

//Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

//Make each worksheet to fit to one page when converting
workbook.ConverterSetting.SheetFitToPage = true;

//Set margins
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
PageSetup pageSetup = workbook.Worksheets[i].PageSetup;
pageSetup.TopMargin = 0.3;
pageSetup.BottomMargin = 0.3;
pageSetup.LeftMargin = 0.3;
pageSetup.RightMargin = 0.3;
}

//Save to PDF
workbook.SaveToFile("ExcelToPdf.pdf", FileFormat.PDF);
}
}
}

[VB.NET]

Imports Spire.Xls

Namespace ConvertWorkbookToPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()

'Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

'Make each worksheet to fit to one page when converting
workbook.ConverterSetting.SheetFitToPage = True

'Set margins
Dim i As Integer
For i = 0 To workbook.Worksheets.Count- 1 Step i + 1
Dim pageSetup As PageSetup = workbook.Worksheets(i).PageSetup
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3
Next

'Save to PDF
workbook.SaveToFile("ExcelToPdf.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace

Convert a Specified Worksheet to PDF in C# and VB.NET

Sometimes, there is only one worksheet that you would like to share with others, you can save it out as a separate PDF document. Below are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Set the page margins through TopMargin, BottomMargin, LeftMargin and RightMargin properties under PageSetup object.
  • Save the worksheet to PDF using Worksheet.SaveToPdf() method.
using Spire.Xls;

namespace ConvertWorksheetToPdf
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();

//Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx");

//Set worksheets to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = true;

//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];

//Set margins
PageSetup pageSetup = worksheet.PageSetup;
pageSetup.TopMargin = 0.3;
pageSetup.BottomMargin = 0.3;
pageSetup.LeftMargin = 0.3;
pageSetup.RightMargin = 0.3;

//Save to PDF
worksheet.SaveToPdf("WorksheetToPdf.pdf");
}
}
}

[VB.NET]

Imports Spire.Xls

Namespace ConvertWorksheetToPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Workbook instance
Dim workbook As Workbook = New Workbook()

'Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx")

'Set worksheets to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True

'Get the first worksheet
Dim worksheet As Worksheet = workbook.Worksheets(0)

'Set margins
Dim pageSetup As PageSetup = worksheet.PageSetup
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3

'Save to PDF
worksheet.SaveToPdf("WorksheetToPdf.pdf")
End Sub
End Class
End Namespace

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.