Microsoft® Excel グラフを作成し、C++ 経由で画像に変換します

Excel ドキュメント グラフを画像に変換し、C++ ベースのアプリケーション内で円グラフ、ピラミッド グラフ、折れ線グラフ、バブル チャートなどのグラフを作成します。

 

Excel グラフを使用すると、全体像を把握し、正しい意思決定を行うためにデータを簡単に分析できます。 C++ エクセルライブラリ によってリストされたさまざまなグラフの作成をサポートします。 列挙型 Aspose::Cells::チャート::ChartType 面グラフ、棒グラフ、円グラフ、ピラミッド グラフ、折れ線グラフ、バブル チャートが含まれます。さらに、チャートの画像への変換については、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();