将工作表转换为不同的图像格式
将工作表转换为图像
工作表包含您要分析的数据。例如,工作表可以包含参数、总计、百分比、异常和计算。
作为开发人员,您可能需要将工作表显示为图像。例如,您可能需要在应用程序或网页中使用工作表的图像。您可能希望将图像插入到 Microsoft Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档类型中。简而言之,您希望将工作表呈现为图像,以便您可以在其他地方使用它。
Aspose.Cells 支持将Excel工作表转换为图片。要使用此功能,您需要导入Aspose.Cells.Rendering命名空间到您的程序或项目。它有几个有价值的渲染和打印类,例如,表单渲染, IImageOrPrint选项和别的。
Aspose.Cells.Rendering.ISheetRender
类表示要呈现为图像的工作表。它有一个重载方法,印象,可以将工作表转换为具有不同属性或选项的图像文件。支持多种图像格式,例如 BMP、PNG、GIF、JPG、JPEG、TIFF、EMF。
以下代码片段显示了如何将 Excel 文件中的工作表转换为图像文件。
PNG 格式
请看下面的示例代码,其示例 Excel 文件 和输出 PNG 图片.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify the image format. | |
imgOptions->SetImageFormat(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetPng()); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get page count. | |
Aspose::Cells::Systems::Int32 pageCount = sr->GetPageCount(); | |
// Create string builder object for string concatenations. | |
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> sb = new Aspose::Cells::Systems::Text::StringBuilder(); | |
// 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->Clear(); | |
sb->Append(outDir); | |
sb->Append((StringPtr)new String("outputConvertingWorksheetToImagePNG_")); | |
sb->Append(i); | |
sb->Append((StringPtr)new String(".png")); | |
// Get the output image path. | |
StringPtr outputPNG = sb->ToString(); | |
// Convert worksheet to png image. | |
sr->ToImage(i, outputPNG); | |
} |
TIFF 格式
请看下面的示例代码,其示例 Excel 文件 和输出 TIFF 图片.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify the image format. | |
imgOptions->SetImageFormat(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetTiff()); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get the output image path. | |
StringPtr outputTiff = outDir->Append((StringPtr)new String("outputConvertingWorksheetToImageTiff.tiff")); | |
// Convert worksheet to tiff image. | |
sr->ToTiff(outputTiff); |
将工作表转换为 SVG
SVG 代表可缩放矢量图形。 SVG是基于XML标准的二维矢量图形规范。它是一个开放标准,自 1999 年以来一直由万维网联盟 (W3C) 开发。
Aspose.Cells for C++ 从版本 18.5.0 开始可以将工作表转换为 SVG 图片。
要使用此功能,请将 Aspose.Cells.Rendering
命名空间导入您的程序或项目。它有几个用于渲染和打印的有价值的类,例如 ISheetRender
、IImageOrPrintOptions
等。
Aspose.Cells.Rendering.IImageOrPrintOptions
类指定工作表将以 SVG 格式保存。以下代码片段显示如何将 Excel 文件中的工作表转换为 SVG 图像文件
请看下面的示例代码,其示例 Excel 文件 和输出 SVG 图片.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Specify the save format. | |
imgOptions->SetSaveFormat(Aspose::Cells::SaveFormat::SaveFormat_SVG); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get page count. | |
Aspose::Cells::Systems::Int32 pageCount = sr->GetPageCount(); | |
// Create string builder object for string concatenations. | |
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> sb = new Aspose::Cells::Systems::Text::StringBuilder(); | |
// 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->Clear(); | |
sb->Append(outDir); | |
sb->Append((StringPtr)new String("outputConvertingWorksheetToImageSVG_")); | |
sb->Append(i); | |
sb->Append((StringPtr)new String(".svg")); | |
// Get the output image path. | |
StringPtr outputSVG = sb->ToString(); | |
// Convert worksheet to tiff image. | |
sr->ToImage(i, outputSVG); | |
} |