使用 ImageOrPrintOptions 的 PageIndex 和 PageCount 属性按顺序呈现页面
Contents
[
Hide
]
使用ImageOrPrintOptions的PageIndex和PageCount属性呈现页面序列
您可以使用Aspose.Cells将Excel文件的一系列页面渲染为图像,使用ImageOrPrintOptions.PageIndex和ImageOrPrintOptions.PageCount属性。当工作表中有许多页面(例如成千上万)但您只想渲染其中的一些页面时,这些属性非常有用。这不仅可以节省处理时间,还可以节省渲染过程的内存消耗。
以下示例代码加载示例Excel文件,并仅使用ImageOrPrintOptions.PageIndex和ImageOrPrintOptions.PageCount属性渲染第4、5、6和7页。以下是示例代码生成的渲染页的图像。
![]() |
![]() |
---|---|
![]() |
![]() |
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |