Microsoft® Excel 차트를 생성하고 C++을 통해 이미지로 변환

C++ 기반 애플리케이션 내에서 Excel 문서 차트를 이미지로 변환하고 원형, 피라미드, 꺾은선형 및 거품형 차트를 포함한 차트를 생성할 수 있습니다.

 

Excel 차트를 사용하면 더 큰 그림을 얻고 데이터를 쉽게 분석하여 올바른 결정을 내릴 수 있습니다. C++ 엑셀 라이브러리 다음과 같이 나열된 다양한 차트 생성을 지원합니다. 열거형 Aspose::Cells::차트::차트 유형 영역형, 막대형, 원형, 피라미드형, 선형 및 거품형 차트를 포함합니다. 또한 차트를 이미지로 변환하려면 API에서 이미지로 필요한 이미지 형식으로 변환합니다.

Excel 차트 만들기

Excel 차트를 만드는 과정은 워크북 수업 원하는 것을 선택하고 워크시트 . 다음을 사용하여 차트를 추가합니다. 메소드 추가 차트 유형을 포함한 관련 매개변수를 사용합니다. 인덱스를 통해 차트에 액세스하고 추가하다 차트의 데이터 소스입니다.

C++ Excel 차트를 생성하는 코드
Aspose::Cells::Startup();
// Path of output XLSM file
U16String outputChartTypeCustom = u"sourceFile.xlsm";
// Create a new workbook
Workbook wkb;
// Get first worksheet which is created by default
Worksheet wks = wkb.GetWorksheets().Get(0);
// Adding sample values to cells
wks.GetCells().Get(u"A1").PutValue(50);
wks.GetCells().Get(u"A2").PutValue(100);
wks.GetCells().Get(u"A3").PutValue(150);
wks.GetCells().Get(u"B1").PutValue(4);
wks.GetCells().Get(u"B2").PutValue(20);
wks.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = wks.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the ODS file
wkb.Save(outputChartTypeCustom);
Aspose::Cells::Cleanup();

차트를 이미지로 변환

차트 변환 과정은 먼저 위의 코드를 이용하여 해당 유형의 차트를 생성하거나 해당 시트에서 접근합니다. 이미지의 출력 저장 경로를 정의하고 ToImage 메서드를 사용하여 변환합니다.

C++ Excel 차트를 변환하는 코드
Aspose::Cells::Startup();
// Output directory path
U16String outDir = u"..\\OutputDirectory\\";
// Path of output image file
U16String outputChartImage = outDir + u"out1image.png";
// Create a new workbook
Workbook wkb;
// Get first worksheet which is created by default
Worksheet wks = wkb.GetWorksheets().Get(0);
// Adding sample values to cells
wks.GetCells().Get(u"A1").PutValue(50);
wks.GetCells().Get(u"A2").PutValue(100);
wks.GetCells().Get(u"A3").PutValue(150);
wks.GetCells().Get(u"B1").PutValue(4);
wks.GetCells().Get(u"B2").PutValue(20);
wks.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = wks.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the chart to image file
chart.ToImage(outputChartImage, ImageType::Png);
Aspose::Cells::Cleanup();