图像

将工作簿转换为TIFF

Excel文件可以包含多个带有多个页面的工作表。WorkbookRender允许您将Excel转换为具有多个页面的TIFF。您还可以控制TIFF的多个选项,如压缩颜色深度,分辨率(水平分辨率垂直分辨率)。

以下代码片段显示了如何将Excel转换为具有多个页面的TIFF。源Excel文件生成的TIFF图像附在此供参考。

from aspose.cells import Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, TiffCompression, WorkbookRender
wb = Workbook("workbook-to-tiff-with-mulitiple-pages.xlsx")
imgOptions = ImageOrPrintOptions()
imgOptions.image_type = ImageType.TIFF
# set Resolution to 200
imgOptions.horizontal_resolution = 200
imgOptions.vertical_resolution = 200
# set TIFF compression to Lzw.
imgOptions.tiff_compression = TiffCompression.COMPRESSION_LZW
workbookRender = WorkbookRender(wb, imgOptions)
workbookRender.to_image("workbook-to-tiff-with-mulitiple-pages.tiff")

将工作表转换为图像

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

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

Aspose.Cells for Python via .NET支持将Excel工作表转换为图像。要使用此功能,您需要将aspose.cells.rendering命名空间导入到您的程序或项目中。它具有几个有价值的类用于渲染和打印,例如SheetRenderImageOrPrintOptionsWorkbookRender等。

SheetRender 类代表要渲染为图像的工作表。 它有一个重载方法,to_image,可以将工作表转换为具有不同属性或选项的图像文件。 它返回一个 System.Drawing.Bitmap 对象,您可以将图像文件保存到磁盘或流中。 支持几种图像格式,例如 BMP、PNG、GIF、JPG、JPEG、TIFF、EMF。

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

from aspose.cells import Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
book = Workbook(sourceDir + "sampleConvertWorksheetToImageByPage.xlsx")
sheet = book.worksheets[0]
options = ImageOrPrintOptions()
options.horizontal_resolution = 200
options.vertical_resolution = 200
options.image_type = ImageType.TIFF
# Sheet2Image By Page conversion
sr = SheetRender(sheet, options)
for j in range(sr.page_count):
sr.to_image(j, outputDir + "outputConvertWorksheetToImageByPage_" + str(j + 1) + ".tif")

将工作表转换为SVG

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

Aspose.Cells for Python via .NET自7.1.0版以来一直能够将工作表转换为SVG图像。以下代码片段显示了如何将Excel文件中的工作表转换为SVG图像文件。

from aspose.cells import SheetType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiate a workbook
workbook = Workbook()
# Put sample text in the first cell of first worksheet in the newly created workbook
workbook.worksheets[0].cells.get("A1").value = "DEMO TEXT ON SHEET1"
# Add second worksheet in the workbook
workbook.worksheets.add(SheetType.WORKSHEET)
# Set text in first cell of the second sheet
workbook.worksheets[1].cells.get("A1").value = "DEMO TEXT ON SHEET2"
# Set currently active sheet incex to 1 i.e. Sheet2
workbook.worksheets.active_sheet_index = 1
# Save workbook to SVG. It shall render the active sheet only to SVG
workbook.save(outputDir + "ConvertWorksheetToSVG_out.svg")

高级主题