Salva tabella pivot nel file ODS

Aspose.Cells for JavaScript tramite C++ offre la possibilità di salvare la tabella pivot in file ODS. Per fare questo, basta convertire un workbook con tabella pivot esistente o crearne una nuova e salvare il file in formato ODS. Assicurati di chiamare PivotTable.calculateData prima di salvare per garantire che la tabella pivot venga renderizzata nel file ODS di output. Il seguente esempio di codice dimostra come salvare la tabella pivot in un file ODS.

Codice di esempio

<!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>

Il file di output generato dal codice precedente è allegato per il tuo riferimento.

File ODS di output