将工作表转换为不同的图像格式

将工作表转换为图像

工作表包含您想要分析的数据。例如,工作表可以包含参数、总计、百分比、异常和计算。

作为开发人员,您可能需要将工作表呈现为图像。例如,您可能需要在应用程序或网页中使用工作表的图像。您可能希望将图像插入到 Microsoft Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档类型中。简而言之,您希望将工作表呈现为图像,以便在其他地方使用它。

Aspose.Cells支持将Excel工作表转换为图像。要使用此功能,您需要将Aspose.Cells.Rendering命名空间导入到您的程序或项目中。它具有几个有价值的类用于呈现和打印,例如SheetRenderImageOrPrintOptions等。

Aspose.Cells.Rendering.ISheetRender类表示要呈现为图像的工作表。它有一个重载的方法ToImage,可以使用不同的属性或选项将工作表转换为图像文件。支持多种图像格式,例如BMP、PNG、GIF、JPG、JPEG、TIFF、EMF。

以下代码片段显示了如何将Excel文件中的工作表转换为图像文件。

PNG格式

请参阅以下示例代码、其示例Excel文件输出的PNG图像

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格式

请参阅以下示例代码、其示例Excel文件输出的TIFF图像

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();

将工作表转换为SVG

SVG代表可缩放矢量图形。SVG是基于XML标准的二维矢量图形规范。自1999年以来,它一直是由万维网联盟(W3C)开发的开放标准。

Aspose.Cells for C++从18.5.0版本开始能够将工作表转换为SVG图像。

要使用此功能,请将Aspose.Cells.Rendering命名空间导入到您的程序或项目中。它具有几个有价值的类用于呈现和打印,例如ISheetRenderIImageOrPrintOptions等。

Aspose.Cells.Rendering.IImageOrPrintOptions类指定工作表将以SVG格式保存。以下代码片段显示了如何将Excel文件中的工作表转换为SVG图像文件。

请参阅以下示例代码、其示例Excel文件输出的SVG图像

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();