Conversion in WPF Spreadsheet (SfSpreadsheet)
23 Jul 20262 minutes to read
This section explains how to convert a workbook in SfSpreadsheet into an image, PDF, and HTML.
Convert to Image
SfSpreadsheet supports converting a worksheet into an image of type Bitmap or Metafile based on the specified range of rows and columns, with all basic formats preserved. Use the ConvertToImage method of IWorksheet to perform the conversion.
IWorksheet sheet = spreadsheet.Workbook.ActiveSheet;
sheet.UsedRangeIncludesFormatting = false;
int lastRow = sheet.UsedRange.LastRow + 1;
int lastColumn = sheet.UsedRange.LastColumn + 1;
System.Drawing.Image image = sheet.ConvertToImage(1, 1, lastRow, lastColumn, ImageType.Bitmap, null);
image.Save("Sample.png", ImageFormat.Png);
System.Diagnostics.Process.Start("Sample.png");Convert to PDF
SfSpreadsheet supports exporting the Excel workbook to PDF using ExcelToPdfConverter.
For converting the Excel sheet to PDF, “Syncfusion.ExcelToPDFConverter.Base.dll” and “Syncfusion.Pdf.Base.dll” references should be added.
Export the Excel workbook as a PDF document using the Convert method of the ExcelToPdfConverter class in the Syncfusion.ExcelToPdfConverter namespace.
ExcelToPdfConverter converter = new ExcelToPdfConverter(spreadsheet.Workbook);
// Initialize the PdfDocument
PdfDocument pdfDoc = new PdfDocument();
// Initialize the ExcelToPdfConverter settings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
settings.LayoutOptions = LayoutOptions.NoScaling;
//Assign the PdfDocument to the TemplateDocument property of ExcelToPdfConverterSettings
settings.TemplateDocument = pdfDoc;
settings.DisplayGridLines = GridLinesDisplayStyle.Invisible;
// Convert Excel document into PDF document
pdfDoc = converter.Convert(settings);
// Save the PDF file
pdfDoc.Save("Sample.pdf");
System.Diagnostics.Process.Start("Sample.pdf");Convert to HTML
SfSpreadsheet supports converting the Excel workbook into an HTML page using the SaveAsHtml method on the underlying IWorkbook.
spreadsheet.Workbook.SaveAsHtml("Sample.html", HtmlSaveOptions.Default);
System.Diagnostics.Process.Start("Sample.html");Note: You can refer to our WPF Spreadsheet Editor feature tour page for its groundbreaking feature representations. You can also explore our WPF Spreadsheet example to know how to render and configure the spreadsheet.