在此示例中,我们将了解如何使用 C++** 在 Excel 文件中创建饼图**。您可以在 C++ 中以编程方式在 Microsoft Windows、Linux 等上的 Excel 工作表中插入饼图。
使用 C++ 在 Excel 文件中创建饼图的步骤
- 使用 NuGet 包管理器配置 Aspose.Cells.Cpp 包
- 添加对 Aspose::Cells 命名空间的引用
- 初始化 Workbook 类对象以创建新的 Excel 文件
- 向单元格添加样本值
- 添加系列集合并设置类别数据
- 使用 IDataLabels 接口格式化数据标签
- 在 C++ 中插入饼图后保存输出 Excel 文件
在下面的示例中,您将学习如何使用 C++ 在 Excel 中插入饼图。只需使用几个 API 调用,即可初始化一个空白工作簿并 使用 C++ 在 Excel 文件中制作饼图。
使用 C++ 在 Excel 中插入饼图的代码
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelPieChart | |
{ | |
void CreateExcelPieChart() | |
{ | |
// Set the license for Aspose.Cells API for creating pie chart | |
intrusive_ptr<License> CellChartLicense = new License(); | |
CellChartLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Instantiate the Workbook object to load Excel file for inserting pie chart | |
intrusive_ptr<IWorkbook> ChartWorkbook = Factory::CreateIWorkbook(); | |
//Accessing a worksheet using its index | |
intrusive_ptr<IWorksheet> ChartWorksheet = ChartWorkbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Adding sample values to cells | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Quarters"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("1st_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("2nd_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("3rd_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("4th_Qtr"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue("Sales"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("6.3"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("3.1"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("2.2"); | |
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("1.9"); | |
// Adding a Pie chart to the worksheet in the workbook | |
int chartIndex = ChartWorksheet->GetICharts()->Add(ChartType::ChartType_Pie, 10, 2, 34, 13); | |
// Accessing the instance of the newly created Pie chart | |
intrusive_ptr<Aspose::Cells::Charts::IChart> PieChart = ChartWorksheet->GetICharts()->GetObjectByIndex(chartIndex); | |
// Adding SeriesCollection to the chart from "A2" to "B5" | |
PieChart->GetNISeries()->Add(new String("A2:B5"), true); | |
PieChart->GetNISeries()->SetCategoryData(new String("A2:A5")); | |
PieChart->GetITitle()->SetText(new String("Sales By Quarter")); | |
PieChart->GetITitle()->GetIFont()->SetColor(Systems::Drawing::Color::GetBlue()); | |
PieChart->GetITitle()->GetIFont()->SetBold(true); | |
PieChart->GetITitle()->GetIFont()->SetSize(11); | |
// Format the datalebels of all series | |
for (int i = 0; i < PieChart->GetNISeries()->GetCount(); i++) | |
{ | |
intrusive_ptr<IDataLabels> dataLabels = PieChart->GetNISeries()->GetObjectByIndex(i)->GetIDataLabels(); | |
dataLabels->SetShowValue(true); | |
dataLabels->SetShowPercentage(true); | |
} | |
// Save the output Excel file with newly created pie chart | |
ChartWorkbook->Save(new String ("output.xlsx")); | |
} | |
}; |
您可以通过插入图表值并以编程方式格式化数据标签,在 Excel 文件中使用 C++ 添加饼图。您无需安装 MS Excel 或任何其他工具即可轻松创建饼图。在前面的示例中,我们探索了 如何在 C++ 中从 Excel 文件中删除公式,其中包含从 Excel 文件中删除公式的示例。