使用 ImageOrPrintOptions 的 PageIndex 和 PageCount 属性渲染页面序列

使用 ImageOrPrintOptions 的 PageIndex 和 PageCount 属性渲染页面序列

您可以使用 Aspose.Cells 将 Excel 文件的一系列页面呈现为图像ImageOrPrintOptions.PageIndexImageOrPrintOptions.PageCount特性。当您的工作表中有很多页面(例如数千页)但您只想呈现其中的一些页面时,这些属性很有用。这不仅会节省处理时间,还会节省渲染过程的内存消耗。

以下示例代码加载示例 Excel 文件并使用ImageOrPrintOptions.PageIndexImageOrPrintOptions.PageCount特性。以下是示例代码生成的渲染页面图片。

待办事项:图片_替代_文本 待办事项:图片_替代_文本
待办事项:图片_替代_文本 待办事项:图片_替代_文本

示例代码

source_directory = "Examples/SampleFiles/SourceDirectory/"
output_directory = "Examples/SampleFiles/OutputDirectory/"
# Load the Sample Workbook
workbook = Workbook(source_directory + "sampleImageOrPrintOptions_PageIndexPageCount.xlsx")
# Access the first worksheet
worksheet = workbook.getWorksheets().get(0)
# Specify image or print options
# We want to print pages 4, 5, 6, 7
imageOrPrintOptions = ImageOrPrintOptions()
imageOrPrintOptions.setPageIndex(3)
imageOrPrintOptions.setPageCount(4)
imageOrPrintOptions.setImageFormat(ImageFormat.getPng())
# Create sheet render object
sheetRender = SheetRender(worksheet, imageOrPrintOptions)
# Print all the pages as images
i = imageOrPrintOptions.getPageIndex()
while i < sheetRender.getPageCount():
sheetRender.toImage(i, output_directory + "outputImage-" + str(i+1) + ".png")
i += 1