Excel File Conversion

DynamicPDF Converter supports Excel file conversion. The following example illustrates converting an Excel worksheet with advanced conversion options.

ExcelConverter

Use the ExcelConverter class to convert Excel spreadsheets to PDF. The ExcelConverter class has properties, methods, and events for conversion.

The ExcelConverter class supports converting using a file path or using a byte array.

Events include ExcelConverter.Completed, ExcelConverter.DocumentParsing, ExcelConverter.ProgressChanged.

ExcelConversionOptions

Set properties for the ExcelConverter class using the ExcelConversionOptions class. The ExcelConversionOptions class has the following properties:

These properties in addition to the inherited ConversionOptions class's properties.

Excel to PDF with advanced options

In this example, you first set the worksheet level options using the DocumentParsing event of the ExcelConverter class. The initial ExcelConversionOptions class is set with a page size of A4. The page size is then changed to Letter for the specific worksheet named Sheet 1 in the event.

 private static void ExcelDemo(string inPath, string outPath)
 {
     ExcelConversionOptions excelOptions = new ExcelConversionOptions(ExcelPageSize.A4, PageOrientation.Portrait, 36);
     ExcelConverter excelConverter = new ExcelConverter(inPath, excelOptions);
     excelConverter.DocumentParsing += excelConverter_DocumentParsing;
     excelConverter.Convert(outPath);
 }  
                        
 static void excelConverter_DocumentParsing(object sender, ExcelDocumentParsingEventArgs excelDocumentParsing)
 {
     foreach (var sheet in excelDocumentParsing.Worksheets)
     {
         if (sheet.SheetName != "Sheet 1")
         {
             sheet.ConversionOptions.PageSize = ExcelPageSize.Letter;
         }
     }
 }
 Private Sub ExcelDemo(ByVal inPath As String, ByVal outPath As String)
         Dim excelOptions As ExcelConversionOptions = New ExcelConversionOptions(ExcelPageSize.A4, PageOrientation.Portrait, 40)
         Dim excelConverter As ExcelConverter = New ExcelConverter(inPath, excelOptions)
         AddHandler excelConverter.DocumentParsing, AddressOf excelConverter_DocumentParsing
         excelConverter.Convert(outPath)
 End Sub
                        
 Private Sub excelConverter_DocumentParsing(sender As Object, ExcelDocumentParsing As ExcelDocumentParsingEventArgs)
         For Each sheet In ExcelDocumentParsing.Worksheets
             If sheet.SheetName = "Sheet 1" Then
                 sheet.ConversionOptions.PageSize = ExcelPageSize.Letter
             End If
         Next
 End Sub

GitHub Project

Refer to the converter-dotnet-core example project on GitHub for examples using C# and Visual Basic.

C# Visual Basic GitHub
ExcelFileConversion.cs ExcelFileConversion.vb https://github.com/DynamicPDF/converter-dotnet-core

In this topic