在这个例子中,我们将探索如何在 C++ 中**将 Excel 图表转换为 JPEG **。图表是表示信息的好方法,您可以在 MS Excel 中添加图表。您可以在任何操作系统(如 Microsoft Windows 和 Linux 等)中使用简单的 API 调用将 Excel 中的图表保存为 C++ 中的 JPEG。
在 C++ 中将 Excel 图表转换为 JPEG 的步骤
- 使用 NuGet 包管理器工具安装 Aspose.Cells.Cpp
- 包括对 Aspose::Cells 命名空间的引用
- 实例化 Workbook Class 对象并加载其中包含图表的 Excel 文件
- 实例化 Chart Class 对象以访问所选工作表内的图表
- 在 C++ 中将 XLSX 图表保存为 JPEG
使用以下简单示例,您可以在几个 API 调用中非常快速轻松地将 Excel 图表转换为 C++ 中的 JPG*。您也可以使用 C++ 将图表导出为 PNG 和 BMP 图像格式。
在 C++ 中将 Excel 图表转换为 JPEG 的代码
#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); | |
} | |
}; |
在前面的示例中,我们了解了 如何使用 C++ 将 XLSX 转换为 XPS。这个例子的重点是如何在 C++ 中将 XLSX 图表转换为 JPEG。