Ottimizzazione dell uso della memoria durante il lavoro con grandi file contenenti grandi set di dati

Ottimizzazione della Memoria

Il seguente esempio mostra come ottimizzare l’uso della memoria durante il lavoro con grandi quantità di dati in Aspose.Cells for JavaScript tramite C++.

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Optimize Memory Usage 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, MemorySetting } = 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 or leave empty to create a new one.</p>';
                // Allow creating a new workbook even if no file selected; return only if user explicitly requires file.
            }

            // If a file is provided, open it; otherwise create a new workbook
            let workbook;
            if (fileInput.files.length) {
                const file = fileInput.files[0];
                const arrayBuffer = await file.arrayBuffer();
                workbook = new Workbook(new Uint8Array(arrayBuffer));
            } else {
                workbook = new Workbook();
            }

            // apply the setting to existing "Sheet1"
            workbook.worksheets.get(0).cells.memorySetting = MemorySetting.MemoryPreference;

            // apply the setting globally
            workbook.settings.memorySetting = MemorySetting.MemoryPreference;

            // Saving the modified Excel file
            const outputData = workbook.save(SaveFormat.Xlsx);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'out.xlsx';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Modified Excel File';

            document.getElementById('result').innerHTML = '<p style="color: green;">Memory settings applied successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>

Attenzione

L’opzione predefinita, MemorySetting.Normal è applicata per tutte le versioni. Per alcune situazioni, come la costruzione di un workbook con un grande set di dati per le celle, l’opzione MemorySetting.MemoryPreference potrebbe ottimizzare l’uso della memoria e ridurre il costo della memoria per l’applicazione. Tuttavia, questa opzione potrebbe degradare le prestazioni in alcuni casi speciali come segue.

  1. Accesso Casuale e Ripetuto alle Celle: La sequenza più efficiente per accedere alla collezione di celle è cella per cella in una riga e poi riga per riga. In particolare, se si accedono alle righe/celle tramite l’Enumerator acquisito da Cells, RowCollection e Row, le prestazioni sarebbero massimizzate con MemorySetting.MemoryPreference.
  2. Inserimento ed Eliminazione di Celle e Righe: Si noti che se ci sono molte operazioni di inserimento/eliminazione per Celle/Righe, la degradazione delle prestazioni sarà notabile per la modalità MemorySetting.MemoryPreference rispetto alla modalità MemorySetting.Normal.
  3. Operare su Diversi Tipi di Celle: Se la maggior parte delle celle contiene valori di stringa o formule, il costo della memoria sarà lo stesso della modalità MemorySetting.Normal ma se ci sono molte celle vuote o i valori delle celle sono numerici, bool e così via, l’opzione MemorySetting.MemoryPreference offrirà migliori prestazioni.