Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active November 19, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/9f28fd2d21f1593ba5d55cc9bb7faa36 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/9f28fd2d21f1593ba5d55cc9bb7faa36 to your computer and use it in GitHub Desktop.
Convert Excel Spreadsheet to PDF using C#
/*
* Convert the specified Cell Range of specified Excel sheets to PDF format using C#
*/
// Prepare Load options and Range for the source XLSX file
Func<LoadOptions> loadOptions = () => new SpreadsheetLoadOptions
{
ConvertRange = "A1:C20"
};
using (var converter = new Converter(@"path/spreadsheet.xlsx", loadOptions))
{
var convertOptions = new PdfConvertOptions()
{
PageNumber = 2,
PagesCount = 3
// Pages = new System.Collections.Generic.List<int> { 2,3,4}
};
// Save as PDF after conversion
converter.Convert(@"path/cell-range-converted.pdf", convertOptions);
}
/*
* Convert all the Excel sheets to PDF format using C#
*/
// Prepare Load options and Range for the source XLSX file
Func<LoadOptions> loadOptions = () => new SpreadsheetLoadOptions
{
OnePagePerSheet = true
};
using (var converter = new GroupDocs.Conversion.Converter(@"path/spreadsheet.xlsx", loadOptions))
{
// Convert and save the spreadsheet in PDF format
converter.Convert(@"path/all-sheets-converted.pdf", new PdfConvertOptions());
}
/*
* Convert sequence of Excel sheets to PDF format using C#
*/
using (var converter = new GroupDocs.Conversion.Converter(@"path/spreadsheet.xlsx"))
{
// Set the starting sheet number and consecutive sheet count
var convertOptions = new PdfConvertOptions()
{
PageNumber = 2,
PagesCount = 3
};
// Convert and save the spreadsheet in PDF format
converter.Convert(@"path/sequential-sheets-converted.pdf", convertOptions);
}
/*
* Convert the specified list of Excel sheets to PDF format using C#
*/
using (var converter = new GroupDocs.Conversion.Converter(@"path/spreadsheet.xlsx"))
{
// Set the list to sheet numbers on convert
var convertOptions = new PdfConvertOptions()
{
Pages = new System.Collections.Generic.List<int> { 1,3,5}
};
// Convert and save the spreadsheet into PDF format
converter.Convert(@"path/selected-sheets-conversion.pdf", convertOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment