Converti un grafico Excel in immagine con JavaScript tramite C++

Conversione di grafici in immagini

Negli esempi qui, un grafico a torta e un grafico a colonne vengono convertiti in immagini.

Conversione di un grafico a torta in un file immagine

Prima, crea un grafico a torta in Microsoft Excel e poi convertilo in un file immagine con Aspose.Cells for JavaScript tramite C++. Il codice in questo esempio crea un’immagine EMF basata sul grafico a torta nel file Excel modello.

Output: immagine grafico a torta
todo:image_alt_text
  1. Crea un grafico a torta in Microsoft Excel:
    1. Apri un nuovo foglio di lavoro in Microsoft Excel.
    2. Inserisci alcuni dati in un foglio di lavoro.
    3. Crea un grafico a torta in base ai dati.
    4. Salvare il file.
Il file di input.
todo:image_alt_text
  1. Scarica e installa Aspose.Cells:
    1. Scarica Aspose.Cells for JavaScript tramite C++.
    2. Installalo sul tuo computer di sviluppo.

Tutti i componenti Aspose funzionano in modalità di valutazione quando vengono installati per la prima volta. La modalità di valutazione non ha limiti temporali e inserisce solo filigrane nei documenti di output.

  1. Crea un progetto:
    1. Avvia il tuo IDE preferito.
    2. Crea una nuova applicazione console. Questo esempio utilizza un’applicazione JavaScript, ma puoi crearlo usando qualsiasi ambiente di runtime JavaScript.
    3. Aggiungi un riferimento. Questo progetto utilizza Aspose.Cells quindi aggiungi un riferimento a Aspose.Cells for JavaScript tramite C++.
    4. Scrivi il codice che trova e converte il grafico. Qui di seguito c’è il codice utilizzato dal componente per completare il compito. Vengono utilizzate pochissime righe di codice.
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Convert Chart to Image 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, 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');
            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));

            // Get the designer chart (first chart) in the first worksheet of the workbook.
            const chart = workbook.worksheets.get(0).charts.get(0);

            // Convert the chart to an image (EMF) and prepare download link
            const imageData = chart.toImage(AsposeCells.ImageType.Emf);
            const blob = new Blob([imageData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'PieChart.out.emf';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Chart Image';

            document.getElementById('result').innerHTML = '<p style="color: green;">Chart converted to EMF successfully! Click the download link to get the image.</p>';
        });
    </script>
</html>

Conversione di un grafico a colonne in un file immagine

Per prima cosa, crea un grafico a colonne in Microsoft Excel e convertilo in un file immagine, come sopra. Dopo aver eseguito il codice di esempio, viene creato un file JPEG basato sul grafico a colonne nel file Excel di modello.

File di output: un’immagine del grafico a colonne.
todo:image_alt_text
  1. Creare un grafico a colonne in Microsoft Excel:
    1. Apri un nuovo foglio di lavoro in Microsoft Excel.
    2. Inserisci alcuni dati in un foglio di lavoro.
    3. Crea un grafico a colonne basato sui dati.
    4. Salvare il file.
File di input.
todo:image_alt_text
  1. Impostare un progetto, con riferimenti, come descritto sopra.
  2. Convertire il grafico in un’immagine in modo dinamico. Di seguito è riportato il codice utilizzato dal componente per portare a termine il compito. Il codice è simile a quello precedente:
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Convert Chart to Image</title>
    </head>
    <body>
        <h1>Convert Chart to Image</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, ImageType, 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');
            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();

            // Instantiate Workbook from uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Get the designer chart (first chart) in the first worksheet of the workbook.
            const chart = workbook.worksheets.get(0).charts.get(0);

            // Convert the chart to an image (JPEG)
            const imageData = await chart.toImage(ImageType.Jpeg);

            // Create a Blob and provide a download link
            const blob = new Blob([imageData], { type: 'image/jpeg' });
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'ColumnChart.out.jpeg';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Chart Image';

            document.getElementById('result').innerHTML = '<p style="color: green;">Chart converted to image successfully! Click the download link to get the image.</p>';
        });
    </script>
</html>