Enregistrer un tableau croisé dynamique dans un fichier ODS

Aspose.Cells for JavaScript via C++ offre la possibilité de sauvegarder le tableau croisé dynamique dans des fichiers ODS. Pour cela, vous convertissez simplement un classeur avec un tableau croisé dynamique existant ou créez un nouveau tableau croisé dynamique et enregistrez le fichier au format ODS. Assurez-vous d’appeler PivotTable.calculateData avant de sauvegarder pour garantir que le tableau croisé dynamique soit correctement rendu dans le fichier ODS de sortie. Le code d’exemple suivant montre comment sauvegarder un tableau croisé dynamique dans un fichier ODS.

Code d’exemple

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

Le fichier de sortie généré par le code ci-dessus est joint à titre de référence.

Fichier ODS de sortie