Convert an Excel Chart to Image with Node.js via C++

Converting Charts to Images

In the examples here, a pie chart and a column chart are converted to images.

Converting a Pie Chart to an Image File

First, create a pie chart in Microsoft Excel and then convert it to an image file with Aspose.Cells for Node.js via C++. The code in this example creates an EMF image based on the pie chart in the template Microsoft Excel file.

Output: pie chart image
todo:image_alt_text
  1. Create a pie chart in Microsoft Excel:
    1. Open a new workbook in Microsoft Excel.
    2. Input some data into a worksheet.
    3. Create a pie chart based on the data.
    4. Save the file.
The input file.
todo:image_alt_text
  1. Download and install Aspose.Cells:
    1. Download Aspose.Cells for Node.js via C++.
    2. Install it on your development computer.

All Aspose components work in evaluation mode when first installed. The evaluation mode has no time limit and it only injects watermarks into output documents.

  1. Create a project:
    1. Start your preferred IDE.
    2. Create a new console application. This example uses a Node.js application, but you can create it using any JavaScript runtime environment.
    3. Add a reference. This project uses Aspose.Cells so add a reference to Aspose.Cells for Node.js via C++.
    4. Write the code that finds and converts the chart. Below is the code used by the component to accomplish the task. Very few lines of code are used.
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Open the existing excel file which contains the pie chart.
const workbook = new AsposeCells.Workbook(path.join(dataDir, "PieChart.xlsx"));

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

// Convert the chart to an image file.
chart.toImage(path.join(dataDir, "PieChart.out.emf"), AsposeCells.ImageType.Emf);

Converting a Column Chart to an Image File

First, create a column chart in Microsoft Excel and convert it to an image file, as above. After executing the sample code, a JPEG file is created based on the column chart in the template Excel file.

Output file: a column chart image.
todo:image_alt_text
  1. Create a column chart in Microsoft Excel:
    1. Open a new workbook in Microsoft Excel.
    2. Input some data into a worksheet.
    3. Create a column chart based on the data.
    4. Save the file.
Input file.
todo:image_alt_text
  1. Set up a project, with references, as described above.
  2. Convert the chart to an image dynamically. Following is the code used by the component to accomplish the task. The code is similar to the previous one:
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open the existing excel file which contains the column chart.
const workbook = new AsposeCells.Workbook(path.join(dataDir, "ColumnChart.xlsx"));

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

// Convert the chart to an image file.
chart.toImage(path.join(dataDir, "ColumnChart.out.jpeg"), AsposeCells.ImageType.Jpeg);