Worksheet to Image Conversion
27 Jul 20267 minutes to read
Assemblies Required
Refer to the following links for the assemblies and NuGet packages required to convert a worksheet to an image, per platform:
NOTE
Worksheet-to-image conversion can be performed by referring to the Syncfusion.XlsIORenderer.Net.Core NuGet package in UWP platform.
Convert as bitmap
The following code shows how to convert a specified range of rows and columns in the worksheet to a bitmap image. The four integer parameters are firstRow, firstColumn, lastRow, and lastColumn (1-based).
// The following snippet assumes `application` and `worksheet` are already initialized
// (see the "Complete code" tab below for the full setup).
// Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
// Create a new memory stream to save the image
using (Stream stream = new MemoryStream())
{
// Convert worksheet to image and save it to the stream.
worksheet.ConvertToImage(1, 1, 10, 20, stream);
}// The following snippet assumes `sheet` is already initialized (see the
// "Complete code" tab below for the full setup).
// Convert as bitmap
Image image = sheet.ConvertToImage(1, 1, 10, 20);
image.Save("Sample.png", ImageFormat.Png);'The following snippet assumes `sheet` is already initialized (see the
'"Complete code" tab below for the full setup).
'Convert as bitmap
Dim image As Image = sheet.ConvertToImage(1, 1, 10, 20)
image.Save("Sample.png", ImageFormat.Png)Save as stream
The following code snippet shows how to save a sheet as a stream. On Windows, you can choose the output ImageType (such as Bitmap, Metafile, or EMF); on .NET Standard the format is controlled by ExportImageOptions (see the Customizing the output image section below). After ConvertToImage(...) writes to a stream, the stream’s position is at the end; set stream.Position = 0 if you need to read from it afterwards.
// The following snippet assumes `application` and `sheet` are already initialized
// (see the "Complete code" tab below for the full setup).
// Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
// Convert and save as a stream (default image format on cross-platform is PNG).
using (MemoryStream stream = new MemoryStream())
{
sheet.ConvertToImage(1, 1, 10, 20, stream);
}// Convert and save as a stream with a specific ImageType
using (MemoryStream stream = new MemoryStream())
{
sheet.ConvertToImage(1, 1, 10, 20, ImageType.Metafile, stream);
}'Convert and save as a stream with a specific ImageType
Using stream As MemoryStream = New MemoryStream()
sheet.ConvertToImage(1, 1, 10, 20, ImageType.Metafile, stream)
End UsingThe complete code snippet of the previous options is given below.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
// Open the input workbook.
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet sheet = workbook.Worksheets[0];
//Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
#region Save
// Save the used range of the worksheet as a PNG image.
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Image.png"), FileMode.Create, FileAccess.Write))
{
sheet.ConvertToImage(sheet.UsedRange, outputStream);
}
#endregion
}
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
IWorksheet sheet = workbook.Worksheets[0];
// Convert the worksheet range to a bitmap image and save it as PNG.
Image image = sheet.ConvertToImage(1, 1, 10, 20);
image.Save("Sample.png", ImageFormat.Png);
// Convert the worksheet range to a Metafile image and save it to a stream.
using (MemoryStream stream = new MemoryStream())
{
sheet.ConvertToImage(1, 1, 10, 20, ImageType.Metafile, stream);
}
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim sheet As IWorksheet = workbook.Worksheets(0)
' Convert the worksheet range to a bitmap image and save it as PNG.
Dim image As Image = sheet.ConvertToImage(1, 1, 10, 20)
image.Save("Sample.png", ImageFormat.Png)
' Convert the worksheet range to a Metafile image and save it to a stream.
Using stream As MemoryStream = New MemoryStream()
sheet.ConvertToImage(1, 1, 10, 20, ImageType.Metafile, stream)
End Using
End UsingA complete working example to convert an Excel worksheet to an image in C# is present on this GitHub page.
NOTE
- Instance of XlsIORenderer class is mandatory to convert the worksheet to image using .NET Standard assemblies.
- In .NET Standard, the Image format and quality can be specified using the ExportImageOptions class. By default the ImageFormat is set to PNG and ScalingMode is set to Best.
- Worksheet to image conversion is supported from .NET Framework 2.0 and .NET Standard 1.4 onwards.
Non-Supported Features:
- Subscript/Superscript
- Shrink to fit
- Complex conditional formatting
See also
- Assemblies Information — manual assembly references.
- NuGet Information — install the right package for your target platform.
- Licensing overview — register your license key.