Excel 2016 グラフの読み取りと操作

考えられる使用シナリオ

Aspose.Cells は、Microsoft Excel 2013 以前のバージョンには存在しない Microsoft Excel 2016 チャートの読み取りと操作をサポートします。

Excel 2016 グラフの読み取りと操作

次のサンプル コードは、サンプル Excel ファイル最初のワークシートに Excel 2016 グラフが含まれています。すべてのチャートを 1 つずつ読み取り、チャートの種類に応じてタイトルを変更します。次のスクリーンショットは、コード実行前のサンプル Excel ファイルを示しています。ご覧のとおり、チャート タイトルはすべてのチャートで同じです。

todo:画像_代替_文章

次のスクリーンショットは、出力エクセルファイルコードの実行後。ご覧のとおり、チャートのタイトルはチャートの種類ごとに変更されています。

todo:画像_代替_文章

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\");
//Output directory path
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\");
//Path of input excel file
StringPtr sampleReadAndManipulateExcel2016Charts = srcDir->StringAppend(new String("sampleReadAndManipulateExcel2016Charts.xlsx"));
//Path of output excel file
StringPtr outputReadAndManipulateExcel2016Charts = outDir->StringAppend(new String("outputReadAndManipulateExcel2016Charts.xlsx"));
// Load sample Excel file containing Excel 2016 charts
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleReadAndManipulateExcel2016Charts);
// Access the first worksheet which contains the charts
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Access all charts one by one and read their types
for (int i = 0; i < worksheet->GetICharts()->GetCount(); i++)
{
// Access the chart
intrusive_ptr<IChart> ch = worksheet->GetICharts()->GetObjectByIndex(i);
//Get the chart type
ChartType chartType = ch->GetType();
//Convert chart type enum to string
StringPtr strChartType = NULL;
switch (chartType)
{
case Aspose::Cells::Charts::ChartType_BoxWhisker:
strChartType = new String("BoxWhisker");
break;
case Aspose::Cells::Charts::ChartType_Histogram:
strChartType = new String("Histogram");
break;
case Aspose::Cells::Charts::ChartType_Sunburst:
strChartType = new String("Sunburst");
break;
case Aspose::Cells::Charts::ChartType_Treemap:
strChartType = new String("Treemap");
break;
case Aspose::Cells::Charts::ChartType_Waterfall:
strChartType = new String("Waterfall");
break;
default:
break;
}
// Print chart type
Aspose::Cells::Systems::Console::WriteLine(strChartType);
// Change the title of the charts as per their types
StringPtr strTitle = (StringPtr)(new String("Chart Type is "))->Append(strChartType);
ch->GetITitle()->SetText(strTitle);
}
// Save the workbook
workbook->Save(outputReadAndManipulateExcel2016Charts);

コンソール出力

上記のサンプル コードを、提供されているサンプル Excel ファイルで実行した場合のコンソール出力を次に示します。

 Waterfall

Treemap

Sunburst

Histogram

BoxWhisker