通过C++用JavaScript预览工作簿

打印预览

对于包含数百万页的Excel文件,转换为PDF或图片可能需要很长时间和大量资源。在这种情况下,工作簿和工作表的打印预览功能可能会很有帮助。在转换之前,用户可以检查总页数,然后决定是否转换该文件。本文重点介绍如何使用WorkbookPrintingPreviewSheetPrintingPreview类来统计总页数。

Aspose.Cells提供了打印预览功能。API提供WorkbookPrintingPreviewSheetPrintingPreview类。要生成整个工作簿的打印预览,需要传入WorkbookImageOrPrintOptions对象实例化WorkbookPrintingPreview类,创建它的实例。WorkbookPrintingPreview类提供一个evaluatedPageCount方法,返回预览中的总页数。与WorkbookPrintingPreview类类似,SheetPrintingPreview类用来为特定工作表生成打印预览,创建实例时传入WorksheetImageOrPrintOptions对象,调用其构造函数。SheetPrintingPreview类还提供一个evaluatedPageCount方法,用于返回预览中的总页数。

以下代码片段演示了如何使用WorkbookPrintingPreviewSheetPrintingPreview类,配合示例Excel文件

示例代码

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Printing Preview</title>
    </head>
    <body>
        <h1>Printing Preview Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, ImageOrPrintOptions, WorkbookPrintingPreview, SheetPrintingPreview } = AsposeCells;

        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        document.getElementById('runExample').addEventListener('click', async () => {
            const fileInput = document.getElementById('fileInput');
            if (!fileInput.files.length) {
                document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
                return;
            }

            const file = fileInput.files[0];
            const arrayBuffer = await file.arrayBuffer();

            // Instantiating a Workbook object from the uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Creating image/print options
            const imgOptions = new ImageOrPrintOptions();

            // Workbook printing preview
            const preview = new WorkbookPrintingPreview(workbook, imgOptions);
            const workbookPageCount = preview.evaluatedPageCount;
            console.log("Workbook page count: " + workbookPageCount);

            // Worksheet printing preview for first worksheet
            const preview2 = new SheetPrintingPreview(workbook.worksheets.get(0), imgOptions);
            const worksheetPageCount = preview2.evaluatedPageCount;
            console.log("Worksheet page count: " + worksheetPageCount);

            document.getElementById('result').innerHTML = `<p>Workbook page count: ${workbookPageCount}</p><p>Worksheet page count: ${worksheetPageCount}</p>`;
        });
    </script>
</html>

执行上述示例代码生成的输出如下。

控制台输出

  
Workbook page count: 1  
Worksheet page count: 1  

高级主题