将数据透视表保存为ODS文件

Contents
[ ]

Aspose.Cells for JavaScript通过C++支持将Pivot表保存为ODS格式文件。你只需将包含已有Pivot表的工作簿转换,或创建新的Pivot表,然后以ODS格式保存。确保在保存前调用PivotTable.calculateData,以确保Pivot表在输出的ODS文件中正确渲染。以下代码片段演示了如何将Pivot表保存为ODS文件。

示例代码

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells PivotTable ODS Example</title>
    </head>
    <body>
        <h1>PivotTable Save In ODS 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, SaveFormat, PivotFieldType, Utils } = 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');

            // Create a new workbook or load from selected file
            let workbook;
            if (fileInput.files && fileInput.files.length > 0) {
                const file = fileInput.files[0];
                const arrayBuffer = await file.arrayBuffer();
                workbook = new Workbook(new Uint8Array(arrayBuffer));
            } else {
                workbook = new Workbook();
            }

            const sheet = workbook.worksheets.get(0);
            const cells = sheet.cells;

            let cell = cells.get("A1");
            cell.value = "Sport";
            cell = cells.get("B1");
            cell.value = "Quarter";
            cell = cells.get("C1");
            cell.value = "Sales";
            cell = cells.get("A2");
            cell.value = "Golf";
            cell = cells.get("A3");
            cell.value = "Golf";
            cell = cells.get("A4");
            cell.value = "Tennis";
            cell = cells.get("A5");
            cell.value = "Tennis";
            cell = cells.get("A6");
            cell.value = "Tennis";
            cell = cells.get("A7");
            cell.value = "Tennis";
            cell = cells.get("A8");
            cell.value = "Golf";
            cell = cells.get("B2");
            cell.value = "Qtr3";
            cell = cells.get("B3");
            cell.value = "Qtr4";
            cell = cells.get("B4");
            cell.value = "Qtr3";
            cell = cells.get("B5");
            cell.value = "Qtr4";
            cell = cells.get("B6");
            cell.value = "Qtr3";
            cell = cells.get("B7");
            cell.value = "Qtr4";
            cell = cells.get("B8");
            cell.value = "Qtr3";
            cell = cells.get("C2");
            cell.value = 1500;
            cell = cells.get("C3");
            cell.value = 2000;
            cell = cells.get("C4");
            cell.value = 600;
            cell = cells.get("C5");
            cell.value = 1500;
            cell = cells.get("C6");
            cell.value = 4070;
            cell = cells.get("C7");
            cell.value = 5000;
            cell = cells.get("C8");
            cell.value = 6430;

            const pivotTables = sheet.pivotTables;
            const index = pivotTables.add("=A1:C8", "E3", "PivotTable2");
            const pivotTable = pivotTables.get(index);
            pivotTable.rowGrand = false;
            pivotTable.addFieldToArea(PivotFieldType.Row, 0);
            pivotTable.addFieldToArea(PivotFieldType.Column, 1);
            pivotTable.addFieldToArea(PivotFieldType.Data, 2);
            pivotTable.calculateData();

            const outputData = workbook.save(SaveFormat.Ods);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'PivotTableSaveInODS_out.ods';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download ODS File';

            document.getElementById('result').innerHTML = '<p style="color: green;">Pivot table created and file ready. Click the download link to get the ODS file.</p>';
        });
    </script>
</html>

上述代码生成的输出文件已附上供参考。

输出ODS文件