How to Convert Excel Chart to JPEG in C++

In this example, we will explore how to convert Excel Chart to JPEG in C++. Charts are good way of representing the information and you can add charts in MS Excel. You can save the chart in Excel to JPEG in C++ using simple API calls in any operating systems like Microsoft Windows and Linux etc.

Steps to Convert Excel Chart to JPEG in C++

  1. Install Aspose.Cells.Cpp with NuGet package Manager tool
  2. Include reference to the Aspose::Cells namespace
  3. Instantiate Workbook Class object and load the Excel file with chart inside it
  4. Instantiate the Chart Class object to access the chart inside selected worksheet
  5. Save XLSX Chart to JPEG in C++

Using the following simple example, you can convert Excel chart to JPG in C++ very quickly and easily in few API calls. You may also export the chart as image using C++ to PNG and BMP image formats as well.

Code to Convert Excel Chart to JPEG in C++

#pragma once
#include "Aspose.Cells.h"
class ExcelChart
{
public:
static void ConvertExcelChartToImage()
{
// Set Aspose.Cells API License
intrusive_ptr<License> CellsRenderingLicense = new License();
CellsRenderingLicense->SetLicense(new String("Aspose.Cells.NET.lic"));
// Instantiate the Workbook obkect to load XLSX with chart in it
intrusive_ptr<IWorkbook> ChartToImageWb = Factory::CreateIWorkbook(new String("input.xlsx"));
// Acces the default worksheet with chart in it
intrusive_ptr<IWorksheet> worksheet = ChartToImageWb->GetIWorksheets()->GetObjectByIndex(0);
// Create instnce of Chart class to access the first chart inside selected excel sheet
intrusive_ptr<Aspose::Cells::Charts::IChart> chart = worksheet->GetICharts()->GetObjectByIndex(0);
// Create an instance of ImageOrPrintOptions to set output image type
intrusive_ptr <IImageOrPrintOptions> imageOrPrintOptions = Factory::CreateIImageOrPrintOptions();
imageOrPrintOptions->SetChartImageType(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetJpeg());
// Save XLSX chart as JPEG image
chart->ToImage(new String("ExcelChartToImage.jpg"), imageOrPrintOptions);
}
};

In previous example, we learnt about How to Convert XLSX to XPS using C++. This examples focuses on how to convert XLSX chart to JPEG in C++.

 English