使用 C++ 通过 JavaScript 将 Excel 图表转换为图像

将图表转换为图像

在此示例中,饼图和柱状图被转换为图像。

将饼图转换为图像文件

首先,在 Microsoft Excel 中创建一个饼图,然后使用 Aspose.Cells for JavaScript 通过 C++ 将其转换为图像文件。本示例中的代码基于模板 Microsoft Excel 文件中的饼图创建了一个 EMF 图像。

输出:饼图图像
todo:image_alt_text
  1. 在Microsoft Excel中创建饼图:
    1. 在Microsoft Excel中打开一个新的工作簿。
    2. 在工作表输入一些数据。
    3. 根据数据创建饼图。
    4. 保存文件。
输入文件。
todo:image_alt_text
  1. 下载并安装 Aspose.Cells:
    1. 下载 Aspose.Cells for JavaScript 通过 C++
    2. 在您的开发计算机上安装它。

在首次安装时,所有Aspose组件均以评估模式运行。 评估模式没有时间限制,只会向输出文档中注入水印。

  1. 创建一个项目:
    1. 启动你偏好的 IDE。
    2. 创建一个新的控制台应用程序。本示例使用 JavaScript 应用程序,但你也可以使用任何 JavaScript 运行环境创建。
    3. 添加引用。本项目使用 Aspose.Cells,因此请添加对 Aspose.Cells for JavaScript 通过 C++ 的引用。
    4. 编写查找并转换图表的代码。 以下是组件用于完成任务的代码。 使用的代码行很少。
<!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>

将柱状图转换为图像文件

首先,在Microsoft Excel中创建柱状图并转换为图像文件,如上所述。执行示例代码后,将根据模板Excel文件中的柱状图生成一个JPEG文件。

输出文件:柱状图图像。
todo:image_alt_text
  1. 在Microsoft Excel中创建柱状图:
    1. 在Microsoft Excel中打开一个新的工作簿。
    2. 在工作表输入一些数据。
    3. 根据数据创建柱状图。
    4. 保存文件。
输入文件。
todo:image_alt_text
  1. 配置项目及引用,如上所述。
  2. 动态将图表转换为图像。 以下是组件用于完成任务的代码。 该代码与先前的代码类似。
<!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>