Converting Worksheet to Different Image Formats
Converting Worksheet to Image
Worksheets contain data that you want to analyze. For example, a worksheet can contain parameters, totals, percentages, exceptions, and calculations.
As a developer, you might need to present worksheets as images. For example, you might need to use an image of a worksheet in an application or web page. You might want to insert an image into a Microsoft Word document, a PDF file, a PowerPoint presentation or some other document type. Simply put, you want a worksheet rendered as an image so that you can use it somewhere else.
Aspose.Cells supports converting Excel worksheets to images. To use this feature, you need to import the Aspose.Cells.Rendering namespace to your program or project. It has several valuable classes for rendering and printing, for example, SheetRender, ImageOrPrintOptions and others.
The Aspose.Cells.Rendering.ISheetRender
class represents a worksheet to render as images. It has an overloaded method, ToImage, that can convert a worksheet to image file(s) with different attributes or options. Several image formats are supported, for example, BMP, PNG, GIF, JPG, JPEG, TIFF, EMF.
The following code snippet shows how to convert a worksheet in an Excel file to an image file.
PNG Format
Please see the following sample code, its sample Excel file, and the output PNG Images.
Aspose::Cells::Startup(); | |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx"; | |
// Create an empty workbook. | |
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Create image or print options object. | |
ImageOrPrintOptions imgOptions; | |
// Specify the image format. | |
imgOptions.SetImageType(ImageType::Png); | |
// Specify horizontal and vertical resolution | |
imgOptions.SetHorizontalResolution(200); | |
imgOptions.SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
SheetRender sr(worksheet, imgOptions); | |
// Get page count. | |
int pageCount = sr.GetPageCount(); | |
// Create string builder object for string concatenations. | |
std::string sb; | |
// Render each page to png image one by one. | |
for (int i = 0; i < pageCount; i++) | |
{ | |
// Clear string builder and create output image path with string concatenations. | |
sb = ""; | |
sb += outDir.ToUtf8(); | |
sb += "outputConvertingWorksheetToImagePNG_"; | |
sb += std::to_string(i); | |
sb += ".png"; | |
// Get the output image path. | |
U16String outputPNG(sb.c_str()); | |
// Convert worksheet to png image. | |
sr.ToImage(i, outputPNG); | |
} | |
Aspose::Cells::Cleanup(); |
TIFF Format
Please see the following sample code, its sample Excel file, and the output TIFF Image.
Aspose::Cells::Startup(); | |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx"; | |
// Create an empty workbook. | |
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Create image or print options object. | |
ImageOrPrintOptions imgOptions; | |
// Specify the image format. | |
imgOptions.SetImageType(ImageType::Tiff); | |
// Specify horizontal and vertical resolution | |
imgOptions.SetHorizontalResolution(200); | |
imgOptions.SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
SheetRender sr(worksheet, imgOptions); | |
// Get the output image path. | |
U16String outputTiff = outDir + u"outputConvertingWorksheetToImageTiff.tiff"; | |
// Convert worksheet to tiff image. | |
sr.ToTiff(outputTiff); | |
Aspose::Cells::Cleanup(); |
Converting Worksheet to SVG
SVG stands for Scalable Vector Graphics. SVG is a specification based on XML standards for two-dimensional vector graphics. It is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.
Aspose.Cells for C++ has been able to convert worksheets to SVG image since version 18.5.0.
To use this feature, import the Aspose.Cells.Rendering
namespace to your program or project. It has several valuable classes for rendering and printing, for example, ISheetRender
, IImageOrPrintOptions
, and others.
The Aspose.Cells.Rendering.IImageOrPrintOptions
class specifies that the worksheet will be saved in SVG format. The following code snippet shows how to convert a worksheet in an Excel file to an SVG image file
Please see the following sample code, its sample Excel file, and the output SVG Images.
Aspose::Cells::Startup(); | |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
U16String srcDir(u"..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
U16String outDir(u"..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx"; | |
// Create an empty workbook. | |
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
// Create image or print options object. | |
ImageOrPrintOptions imgOptions; | |
// Specify the image format. | |
imgOptions.SetImageType(ImageType::Svg); | |
// Specify horizontal and vertical resolution | |
imgOptions.SetHorizontalResolution(200); | |
imgOptions.SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
SheetRender sr(worksheet, imgOptions); | |
// Get page count. | |
int pageCount = sr.GetPageCount(); | |
// Create string builder object for string concatenations. | |
std::string sb; | |
// Render each page to png image one by one. | |
for (int i = 0; i < pageCount; i++) | |
{ | |
// Clear string builder and create output image path with string concatenations. | |
sb = ""; | |
sb += outDir.ToUtf8(); | |
sb += "outputConvertingWorksheetToImageSVG_"; | |
sb += std::to_string(i); | |
sb += ".svg"; | |
// Get the output image path. | |
U16String outputSvg(sb.c_str()); | |
// Convert worksheet to svg image. | |
sr.ToImage(i, outputSvg); | |
} | |
Aspose::Cells::Cleanup(); |