Comment exporter des équations Excel vers d autres types d expressions

Parfois, vous pouvez avoir besoin d’exporter les formules Excel vers d’autres formats dans votre code pour répondre à vos besoins professionnels, alors la bibliothèque Aspose.Cells peut répondre à vos besoins. Le contenu suivant présente quelques méthodes pour exporter des formules Excel vers d’autres formats, j’espère que ces méthodes vous seront utiles.

Nous avons préparé un code d’exemple ici pour vous aider à atteindre vos objectifs en utilisant Aspose.Cells. Les fichiers d’échantillon nécessaires sont également fournis.

Fichier d’exemple : Sample.xlsx

Exporter des équations en expressions LaTeX

Si vous souhaitez exporter des équations dans Excel sous forme d’expressions LaTeX, vous pouvez utiliser la méthode EquationNode.toLaTeX().

Le code d’exemple ci-dessous montre comment utiliser la méthode EquationNode.toLaTeX() et insérer les résultats générés dans HTML :

JavaScript-A-LaTeX

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
        <meta charset="UTF-8" />
        <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"></script>
        <script type="text/x-mathjax-config">
            MathJax.Hub.Config({
                tex2jax: {
                    inlineMath: [['$','$'], ['\\(','\\)']],
                    processEscapes: true
                }
            });
        </script>
        <style>
            body { font-family: Arial, sans-serif; margin: 20px; }
            table { border-collapse: collapse; margin-bottom: 20px; }
            td, th { border: 1px solid #ccc; padding: 4px 8px; }
            .sheet-title { margin-top: 16px; font-weight: bold; }
            #downloadLink { display: none; margin-left: 8px; }
            #result p.error { color: red; }
            #result p.success { color: green; }
        </style>
    </head>
    <body>
        <h1>Aspose.Cells Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, SaveFormat, 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');
            const resultDiv = document.getElementById('result');
            const downloadLink = document.getElementById('downloadLink');

            if (!fileInput.files.length) {
                resultDiv.innerHTML = '<p class="error">Please select an Excel file.</p>';
                downloadLink.style.display = 'none';
                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));

            // Build HTML output to display sheet names and cell values (MathJax-enabled)
            let outHtml = '';
            outHtml += '<div>';
            outHtml += '<p class="success">Workbook loaded successfully. Rendering sheet data below. Math expressions (if any) will be processed by MathJax.</p>';

            const sheetCount = workbook.worksheets.count;
            outHtml += `<p>Total sheets: ${sheetCount}</p>`;

            for (let si = 0; si < sheetCount; si++) {
                const sheet = workbook.worksheets.get(si);
                outHtml += `<div class="sheet-section">`;
                outHtml += `<div class="sheet-title">Sheet ${si + 1}: ${sheet.name}</div>`;

                // Get rows/columns counts (limited to a reasonable max to avoid huge output)
                const rows = sheet.cells.rows;
                const cols = sheet.cells.columns;
                let rowCount = rows.count || 0;
                let colCount = cols.count || 0;

                // If counts are zero, still attempt a small scan window (first 50x20)
                const MAX_ROWS = 100;
                const MAX_COLS = 50;

                if (rowCount <= 0) rowCount = Math.min(MAX_ROWS, 50);
                else rowCount = Math.min(rowCount, MAX_ROWS);

                if (colCount <= 0) colCount = Math.min(MAX_COLS, 20);
                else colCount = Math.min(colCount, MAX_COLS);

                outHtml += '<table><thead><tr><th></th>';
                for (let c = 0; c < colCount; c++) {
                    outHtml += `<th>C${c}</th>`;
                }
                outHtml += '</tr></thead><tbody>';

                for (let r = 0; r < rowCount; r++) {
                    let rowHasData = false;
                    let rowHtml = `<tr><th>R${r}</th>`;
                    for (let c = 0; c < colCount; c++) {
                        const cell = sheet.cells.get(r, c);
                        let cellText = '';
                        if (cell) {
                            // Prefer cell.value; fall back to empty string
                            const v = cell.value;
                            if (v !== undefined && v !== null) {
                                cellText = String(v);
                                rowHasData = rowHasData || (cellText.trim() !== '');
                            }
                        }
                        // Escape HTML special chars
                        cellText = cellText
                            .replace(/&/g, '&amp;')
                            .replace(/</g, '&lt;')
                            .replace(/>/g, '&gt;');

                        // Keep inline math delimiters if present so MathJax can render
                        rowHtml += `<td>${cellText}</td>`;
                    }
                    rowHtml += '</tr>';
                    // Only append rows that have any data to avoid huge empty tables
                    if (rowHasData) {
                        outHtml += rowHtml;
                    }
                }

                outHtml += '</tbody></table>';
                outHtml += `</div>`;
            }

            outHtml += '</div>';

            resultDiv.innerHTML = outHtml;

            // Trigger MathJax typeset for dynamically added content (MathJax v2 API)
            if (window.MathJax && MathJax.Hub && typeof MathJax.Hub.Queue === 'function') {
                MathJax.Hub.Queue(["Typeset", MathJax.Hub, resultDiv]);
            }

            // Prepare download of the (unmodified) workbook as an XLSX file
            const outputData = workbook.save(SaveFormat.Xlsx);
            const blob = new Blob([outputData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'output.xlsx';
            downloadLink.style.display = 'inline-block';
            downloadLink.textContent = 'Download Excel File';
        });
    </script>
</html>

Exporter des équations en expressions MathML

Si vous souhaitez exporter des équations dans Excel sous forme d’expressions MathML, vous pouvez utiliser la méthode EquationNode.toMathML().

Le code d’exemple ci-dessous montre comment utiliser la méthode EquationNode.toMathML() et insérer les résultats générés dans HTML :

JavaScript-A-MathML

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

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, SaveFormat } = 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));

            // Access shapes from the first worksheet
            const shapes = workbook.worksheets.get(0).shapes;
            const textBox = shapes.get(0);

            if (textBox instanceof AsposeCells.TextBox) {
                const mathNode = textBox.equationParagraph.child(0);
                const mathML = mathNode.toMathML();

                const htmlContent = '<!DOCTYPE html>\r\n<html lang="en">\r\n<head>\r\n    <meta charset="UTF-8">\r\n    <title>Title</title>\r\n    <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"></script>\r\n</head>\r\n<body>' + mathML + '\r\n</body>\r\n</html>';

                document.getElementById('result').innerHTML = '<p style="color: green;">MathML extracted. Click the download link to save the result.html file.</p>';

                const blob = new Blob([htmlContent], { type: 'text/html' });
                const downloadLink = document.getElementById('downloadLink');
                downloadLink.href = URL.createObjectURL(blob);
                downloadLink.download = 'result.html';
                downloadLink.style.display = 'block';
                downloadLink.textContent = 'Download result.html';
            } else {
                document.getElementById('result').innerHTML = '<p style="color: red;">No TextBox with an equation was found in the first worksheet.</p>';
            }
        });
    </script>
</html>