この例では、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")); | |
} | |
}; |
グラフの値を挿入し、プログラムでデータラベルをフォーマットすることにより、C++を使用してExcelファイルに円グラフを追加できます。 MS Excelやその他のツールをインストールしなくても、円グラフを簡単に作成できます。前の例では、Excelファイルから数式を削除する例を含むC++でExcelファイルから数式を削除する方法を調べました。