C++를 사용하여 Excel 파일에서 원형 차트를 만드는 방법

이 예에서는 **C++를 사용하여 Excel 파일에 파이 차트를 만드는 방법을 이해합니다. C++에서 프로그래밍 방식으로 Microsoft Windows, Linux 등의 Excel 워크시트에 원형 차트를 삽입할 수 있습니다.

C++를 사용하여 Excel 파일에서 원형 차트를 만드는 단계

  1. NuGet 패키지 관리자로 Aspose.Cells.Cpp 패키지 구성
  2. Aspose::Cells 네임스페이스에 대한 참조 추가
  3. Workbook 클래스 개체를 초기화하여 새 Excel 파일 생성
  4. 셀에 샘플 값 추가
  5. 시리즈 컬렉션 추가 및 카테고리 데이터 설정
  6. IDataLabels 인터페이스를 사용하여 데이터 레이블 형식 지정
  7. 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 파일에서 수식을 제거하는 방법를 살펴보았습니다.

 한국인