使用 PageIndex 和 PageCount 属性在 Node.js 中渲染页面序列

可能的使用场景

你可以通过使用 ImageOrPrintOptions.getPageIndex()ImageOrPrintOptions.getPageCount() 属性,将 Excel 文件的页面序列渲染为图像。这些属性在工作表页面很多(比如数千页)时非常有用,只渲染部分页面。这不仅节省处理时间,还能节省渲染过程中的内存消耗。

使用ImageOrPrintOptions的PageIndex和PageCount属性呈现页面序列

以下示例加载 示例 Excel 文件,并只渲染第4、5、6和7页,使用 ImageOrPrintOptions.getPageIndex()ImageOrPrintOptions.getPageCount() 属性。示例代码生成的页面如下。

todo:image_alt_text todo:image_alt_text
todo:image_alt_text todo:image_alt_text

示例代码

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");
// Output directory
const outputDir = path.join(__dirname, "output");
// Load the sample Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "sampleImageOrPrintOptions_PageIndexPageCount.xlsx"));
// Access the first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Specify image or print options
// We want to print pages 4, 5, 6, 7
const opts = new AsposeCells.ImageOrPrintOptions();
opts.setPageIndex(3);
opts.setPageCount(4);
opts.setImageType(AsposeCells.ImageType.Png);
// Create sheet render object
const sheetRender = new AsposeCells.SheetRender(worksheet, opts);
// Print all the pages as images
for (let i = opts.getPageIndex(); i < sheetRender.getPageCount(); i++) {
sheetRender.toImage(i, path.join(outputDir, `outputImage-${i + 1}.png`));
}