使用 ImageOrPrintOptions 的 PageIndex 和 PageCount 属性渲染页面序列
Contents
[
Hide
]
使用 ImageOrPrintOptions 的 PageIndex 和 PageCount 属性渲染页面序列
您可以使用 Aspose.Cells 将 Excel 文件的一系列页面呈现为图像ImageOrPrintOptions.PageIndex和ImageOrPrintOptions.PageCount特性。当您的工作表中有很多页面(例如数千页)但您只想呈现其中的一些页面时,这些属性很有用。这不仅会节省处理时间,还会节省渲染过程的内存消耗。
以下示例代码加载示例 Excel 文件并使用ImageOrPrintOptions.PageIndex和ImageOrPrintOptions.PageCount特性。以下是示例代码生成的渲染页面图片。
![]() |
![]() |
---|---|
![]() |
![]() |
示例代码
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 |