使用 C++ 管理簡報中的圖表活頁簿
概覽
本文說明了如何在 Aspose.Slides 中使用圖表活頁簿。它展示了如何透過活頁簿串流讀寫圖表資料、將活頁簿儲存格作為圖表資料標籤、存取工作表集合,以及為圖表值指定資料來源類型。
它還涵蓋了將外部活頁簿作為圖表資料來源的使用方式。範例示範了如何建立並指派外部活頁簿、取得連結至圖表的外部活頁簿路徑,以及在活頁簿可用時編輯圖表資料。
若活頁簿儲存格代表缺少資料,請參閱控制空儲存格的顯示,了解空儲存格與零的差異,以及可用顯示模式的折線圖比較。
包含隱藏列與欄的資料
使用IChart::set_PlotVisibleCellsOnly來控制圖表是否繪製隱藏工作表列與欄的資料。設定為 true 時僅繪製可見儲存格,設定為 false 時同時包含可見與隱藏儲存格。此設定只影響圖表繪製,並不會隱藏或取消隱藏工作表列或欄。
範例簡報的第一張投影片第一個圖形是一個柱狀圖。內嵌工作表 Sheet1 包含以下來源範圍 A1:C4。第 3 列與 C 欄被隱藏,但其儲存格仍保有值。
| 工作表列 | A: 月份 | B: 零售 | C: 批發(隱藏欄) |
|---|---|---|---|
| 2 | 一月 | 10 | 30 |
| 3(隱藏列) | 二月 | 40 | 60 |
| 4 | 三月 | 20 | 50 |
透過IChartData::get_ChartDataWorkbook取得來源儲存格,並讀取IChartDataCell::get_IsHidden以檢查其隱藏狀態。此屬性唯讀。在此檔案中,B2 為可見,B3 屬於隱藏列,C2 屬於隱藏欄;範例分別印出 False、True、True。
對於此範例,變更繪製設定後請重新整理圖表資料:使用ReadWorkbookStream保留內嵌活頁簿,並以WriteWorkbookStream重新載入。若要包含所有儲存格,亦請使用SetRange還原完整範圍(包括隱藏的二月類別)。僅變更旗標不足以更新此樣本的快取圖表資料與類別標籤。
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <initializer_list>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"hidden-source-data.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
Console::WriteLine(u"B2 hidden: {0}", workbook->GetCell(0, u"B2")->get_IsHidden());
Console::WriteLine(u"B3 hidden: {0}", workbook->GetCell(0, u"B3")->get_IsHidden());
Console::WriteLine(u"C2 hidden: {0}", workbook->GetCell(0, u"C2")->get_IsHidden());
auto workbookStream = chart->get_ChartData()->ReadWorkbookStream();
for (auto visibleOnly : {true, false})
{
chart->set_PlotVisibleCellsOnly(visibleOnly);
// 從內嵌活頁簿重新整理圖表資料。
workbookStream->set_Position(0);
chart->get_ChartData()->WriteWorkbookStream(workbookStream);
if (!visibleOnly)
{
// 還原完整的來源範圍,包括隱藏的類別。
chart->get_ChartData()->SetRange(u"Sheet1!$A$1:$C$4");
}
auto outputPath = visibleOnly ? u"hidden_cells_True.pptx" : u"hidden_cells_False.pptx";
presentation->Save(outputPath, Export::SaveFormat::Pptx);
}
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
此範例會儲存兩個版本的簡報:一個僅保留可見的零售值(10 與 20),另一個保留全部六個值。下列圖片說明兩種繪製模式。第 3 列與 C 欄在兩個內嵌活頁簿中皆保持隱藏。
僅可見儲存格(true) |
全部儲存格(false) |
|---|---|
![]() |
![]() |
隱藏且包含值的儲存格與空儲存格不同。IChart::get_DisplayBlanksAs控制缺少值的顯示方式,並不會包含或排除隱藏的來源資料。請參閱控制空儲存格的顯示以了解範例。
取得圖表的資料範圍
在現有簡報中更新活頁簿資料之前,先檢查來源範圍以確定每個圖表使用的工作表儲存格。IChartData::GetRange會回傳目前的資料範圍,格式為工作表限定的公式,例如 Sheet1!$A$1:$D$5。其中 Sheet1 為工作表名稱,! 為分隔符,$A$1:$D$5 表示包含 A1 到 D5(含)的儲存格。美元符號表示絕對列與欄參照。
此方法僅讀取目前範圍,不會變更圖表或其活頁簿。若圖表未使用活頁簿作為資料來源,會拋出System::InvalidOperationException。更多資訊請參考ChartData API 參考文件。
此範例會開啟簡報,直接檢查每張投影片上的圖形是否為圖表,並印出每個圖表的名稱與來源範圍。若圖表未使用活頁簿,則印出訊息並繼續處理下一個圖表。
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
#include <system/exceptions.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"presentation.pptx");
for (auto slide : IterateOver(presentation->get_Slides()))
{
for (auto shape : IterateOver(slide->get_Shapes()))
{
auto chart = AsCast<IChart>(shape);
if (chart != nullptr)
{
try
{
auto range = chart->get_ChartData()->GetRange();
Console::WriteLine(u"{0}: {1}", chart->get_Name(), range);
}
catch (const InvalidOperationException&)
{
Console::WriteLine(u"{0}: The chart does not use a workbook as its data source.", chart->get_Name());
}
}
}
}
從活頁簿讀寫圖表資料
Aspose.Slides for C++ 提供ReadWorkbookStream與WriteWorkbookStream方法,讓您讀寫圖表資料活頁簿(由 Aspose.Cells 編輯的圖表資料)。注意,圖表資料必須以相同方式組織,或具備類似的結構。
此範例使用第一張投影片第一個圖形的圖表。它將內嵌活頁簿讀入串流,清除既有系列與類別,並將相同的活頁簿寫回。變更僅保留在記憶體中;範例不會儲存簡報。
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"chart.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto chartData = chart->get_ChartData();
auto workbookStream = chartData->ReadWorkbookStream();
chartData->get_Series()->Clear();
chartData->get_Categories()->Clear();
workbookStream->set_Position(0);
chartData->WriteWorkbookStream(workbookStream);
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
驗證活頁簿變更後的圖表版面配置
當您以修改過的活頁簿取代內嵌活頁簿時,圖表仍保留原有的系列與類別集合。此不相符可能導致IChart::ValidateChartLayout因索引超出範圍而失敗。寫回更新的活頁簿前,請先清除既有系列與類別。此範例使用第一張投影片第一個圖形的圖表。註解標記了活頁簿編輯位置;可執行範例寫回原始活頁簿並在記憶體中驗證版面配置。
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/io/memory_stream.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"chart.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto chartData = chart->get_ChartData();
auto workbookStream = chartData->ReadWorkbookStream();
// 在此修改活頁簿串流,例如使用 Aspose.Cells。
chartData->get_Series()->Clear();
chartData->get_Categories()->Clear();
workbookStream->set_Position(0);
chartData->WriteWorkbookStream(workbookStream);
chart->ValidateChartLayout();
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
清除集合可在活頁簿寫回前移除過期的資料參照。更新活頁簿後,請重新建立所需的系列與類別對應關係,然後再使用圖表。
為活頁簿儲存格設定自訂數字格式
使用IChartDataCell::set_CustomNumberFormat控制數值在圖表內嵌活頁簿中的顯示方式。例如,報表可能需要將轉換率以四位小數的百分比顯示,係數以五位小數顯示。兩個值皆保持數值型別,以便後續計算與圖表繪製。
數字格式僅改變顯示,不會改變儲存的值。格式 0.0000% 會將 0.125 顯示為 12.5000%,但不會把儲存的數字改成 12.5。同理,0.00000 會將 0.1234567 顯示為 0.12346,且不會捨入儲存值。若自訂格式為空字串 (""),則使用透過set_PresetNumberFormat 指定的格式。
以下範例建立柱狀圖,並透過IChartData::get_ChartDataWorkbook存取儲存格。工作表 0 的 B2 含轉換率,C2 含係數,兩個儲存格皆供圖表使用。範例亦啟用連結至來源格式的值標籤。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <system/enumerator_adapter.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 270, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 20, 620, 210);
chart->set_HasTitle(false);
chart->get_Axes()->get_VerticalAxis()->set_IsNumberFormatLinkedToSource(false);
chart->get_Axes()->get_VerticalAxis()->set_NumberFormat(u"0.00");
chart->get_Axes()->get_VerticalAxis()->set_IsAutomaticMinValue(false);
chart->get_Axes()->get_VerticalAxis()->set_MinValue(0);
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);
auto categoryCell = workbook->GetCell(0, u"A2", ObjectExt::Box<String>(u"Sample"));
chart->get_ChartData()->get_Categories()->Add(categoryCell);
auto percentageCell = workbook->GetCell(0, u"B2", ObjectExt::Box<double>(0.125));
percentageCell->set_CustomNumberFormat(u"0.0000%");
auto percentageName = workbook->GetCell(0, u"B1", ObjectExt::Box<String>(u"Conversion rate"));
auto percentageSeries = chart->get_ChartData()->get_Series()->Add(percentageName, chart->get_Type());
percentageSeries->get_DataPoints()->AddDataPointForBarSeries(percentageCell);
auto precisionCell = workbook->GetCell(0, u"C2", ObjectExt::Box<double>(0.1234567));
precisionCell->set_CustomNumberFormat(u"0.00000");
auto precisionName = workbook->GetCell(0, u"C1", ObjectExt::Box<String>(u"Coefficient"));
auto precisionSeries = chart->get_ChartData()->get_Series()->Add(precisionName, chart->get_Type());
precisionSeries->get_DataPoints()->AddDataPointForBarSeries(precisionCell);
for (auto series : IterateOver(chart->get_ChartData()->get_Series()))
{
auto labelFormat = series->get_DataPoint(0)->get_Label()->get_DataLabelFormat();
labelFormat->set_ShowValue(true);
labelFormat->set_IsNumberFormatLinkedToSource(true);
}
presentation->Save(u"CustomCellFormats.pptx", Export::SaveFormat::Pptx);
活頁簿儲存格的格式與直接設定於圖表座標軸或資料標籤的格式是分開的。在此範例中,每個標籤皆因呼叫set_ShowValue 並傳入 true,而顯示其來源儲存格的值;同時因呼叫set_IsNumberFormatLinkedToSource 並傳入 true,而遵循來源的數字格式。儲存並重新開啟簡報後,標籤分別顯示 12.5000% 與 0.12346,如下所示。垂直座標軸則使用透過set_NumberFormat 指定的 0.00 格式,且因set_IsNumberFormatLinkedToSource 為 false,不會連結至來源。
若要獨立設定資料標籤的格式,請將其來源連結旗標設為 false,並透過set_NumberFormat 指定格式。此操作不會變更活頁簿儲存格的格式。例如,將獨立標籤格式設為 0.00 會將第一個值顯示為 0.13,而 B2 仍顯示 12.5000%。請參閱圖表資料標籤取得直接標籤格式化的說明。
以下圖片為重新開啟後儲存的簡報所呈現的結果:

將活頁簿儲存格設定為圖表資料標籤
您可以使用活頁簿儲存格的文字作為圖表資料標籤。
此範例在現有簡報的第一張投影片加入一個預設資料的氣泡圖。它使用工作表 0 的 A10:A12 作為第一系列的前三個標籤,啟用來自儲存格的標籤,並儲存更新後的簡報。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDataLabel.h>
#include <DOM/Chart/IDataLabelCollection.h>
#include <DOM/Chart/IDataLabelFormat.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"chart2.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Bubble, 50, 50, 600, 400, true);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLabelValueFromCell(true);
auto firstLabelCell = workbook->GetCell(0, u"A10", ObjectExt::Box<String>(u"Label 0 cell value"));
auto secondLabelCell = workbook->GetCell(0, u"A11", ObjectExt::Box<String>(u"Label 1 cell value"));
auto thirdLabelCell = workbook->GetCell(0, u"A12", ObjectExt::Box<String>(u"Label 2 cell value"));
series->get_Labels()->idx_get(0)->set_ValueFromCell(firstLabelCell);
series->get_Labels()->idx_get(1)->set_ValueFromCell(secondLabelCell);
series->get_Labels()->idx_get(2)->set_ValueFromCell(thirdLabelCell);
presentation->Save(u"resultchart.pptx", Export::SaveFormat::Pptx);
管理工作表
IChartDataWorkbook::get_Worksheets 方法提供對圖表活頁簿中工作表的存取。此範例建立一個預設資料的圓餅圖,並將每個工作表名稱印到主控台。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataWorksheet.h>
#include <DOM/Chart/IChartDataWorksheetCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 500);
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
for (auto i = 0; i < workbook->get_Worksheets()->get_Count(); i++)
{
Console::WriteLine(workbook->get_Worksheets()->idx_get(i)->get_Name());
}
指定資料來源類型
系列點的值也可以直接以數值常量儲存。請參閱使用數值常量設定系列值取得完整的儲存與讀取範例。點值的來源類型與圖表的內部或外部活頁簿設定是分開的;使用數值常量本身不會移除內嵌活頁簿。
此範例建立一個預設資料的 3D 柱狀圖,並以不同資料來源設定兩個系列名稱。第一個名稱使用字串常量;第二個名稱使用工作表 0 的儲存格 C1。DataSourceType 列舉決定每個名稱的來源。範例儲存簡報以更新系列名稱。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/DataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IStringChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Column3D, 50, 50, 600, 400, true);
auto literalName = chart->get_ChartData()->get_Series()->idx_get(0)->get_Name();
literalName->set_DataSourceType(DataSourceType::StringLiterals);
literalName->set_Data(ObjectExt::Box<String>(u"LiteralString"));
auto cellName = chart->get_ChartData()->get_Series()->idx_get(1)->get_Name();
auto nameCell = chart->get_ChartData()->get_ChartDataWorkbook()->GetCell(0, u"C1", ObjectExt::Box<String>(u"NewCell"));
cellName->set_DataSourceType(DataSourceType::Worksheet);
cellName->set_Data(nameCell);
presentation->Save(u"pres.pptx", Export::SaveFormat::Pptx);
偵測不支援的內嵌活頁簿格式
Aspose.Slides 不支援可嵌入於某些圖表的 Excel 二進位活頁簿(.xlsb)格式。您可以在IChartData 上使用get_EmbeddedWorkbookType 方法,結合WorkbookType 列舉,以偵測不支援的格式並跳過那些圖表。此範例檢查現有簡報第一張投影片上的圖形,跳過非圖表圖形,並為每個內嵌 .xlsb 活頁簿的圖表印出診斷訊息。
#include <DOM/Chart/ChartDataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/WorkbookType.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
for (auto shape : IterateOver(slide->get_Shapes()))
{
auto chart = AsCast<IChart>(shape);
if (chart == nullptr)
{
continue;
}
auto chartData = chart->get_ChartData();
auto isInternalWorkbook = chartData->get_DataSourceType() == ChartDataSourceType::InternalWorkbook;
auto isBinaryMacro = chartData->get_EmbeddedWorkbookType() == WorkbookType::WorkbookBinaryMacro;
if (isInternalWorkbook && isBinaryMacro)
{
Console::WriteLine(u"Skipping a chart with an unsupported .xlsb workbook.");
continue;
}
// 在此讀取或修改受支援的圖表活頁簿資料。
}
外部活頁簿
Aspose.Slides 支援將外部活頁簿作為圖表的資料來源。
建立外部活頁簿
使用ReadWorkbookStream與SetExternalWorkbook將內嵌圖表活頁簿匯出為檔案,並將圖表連結至該外部活頁簿。
此範例建立一個預設資料的圓餅圖,匯出其活頁簿。關閉輸出串流後指派外部活頁簿為圖表資料來源,最後儲存已連結的簡報。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/io/memory_stream.h>
#include <system/io/path.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600);
auto workbookPath = IO::Path::GetFullPath(u"externalWorkbook1.xlsx");
auto workbookStream = chart->get_ChartData()->ReadWorkbookStream();
auto fileStream = IO::File::Create(workbookPath);
workbookStream->CopyTo(fileStream);
fileStream->Close();
chart->get_ChartData()->SetExternalWorkbook(workbookPath);
presentation->Save(u"externalWorkbook.pptx", Export::SaveFormat::Pptx);
設定外部活頁簿
使用SetExternalWorkbook方法,可將外部活頁簿指派給圖表作為資料來源。此方法亦可用於更新外部活頁簿的路徑(若該檔案已搬移)。
雖然無法編輯儲存在遠端位置或資源中的活頁簿資料,但仍可將此類活頁簿作為外部資料來源。若提供相對路徑,系統會自動轉換為完整路徑。
此範例使用外部活頁簿,其工作表 Sheet1 在 B1 含系列名稱、A2:A4 含類別名稱、B2:B4 含數值。範例建立圓餅圖、連結活頁簿,並使用SetRange將 A1:B4 映射為一個系列與三個類別。最後儲存帶有連結圖表的簡報。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/path.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600, true);
auto chartData = chart->get_ChartData();
auto workbookPath = IO::Path::GetFullPath(u"externalWorkbook.xlsx");
chartData->SetExternalWorkbook(workbookPath);
chartData->SetRange(u"Sheet1!$A$1:$B$4");
presentation->Save(u"Presentation_with_externalWorkbook.pptx", Export::SaveFormat::Pptx);
SetExternalWorkbook 的 updateChartData 參數控制是否載入活頁簿。
- 當
updateChartData為false時,僅更新活頁簿路徑。圖表資料不會從目標活頁簿載入或更新,因此活頁簿可以不存在。 - 當
updateChartData為true時,圖表資料會從目標活頁簿更新。
以下範例將佔位 URL 指派給 updateChartData 為 false。它保留圓餅圖的預設資料,且在未載入無法使用的活頁簿時仍能儲存簡報。
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50, 50, 400, 600, true);
chart->get_ChartData()->SetExternalWorkbook(u"https://example.com/unavailable-workbook.xlsx", false);
presentation->Save(u"SetExternalWorkbookWithUpdateChartData.pptx", Export::SaveFormat::Pptx);
取得圖表的外部資料來源活頁簿路徑
要識別連結至圖表的活頁簿,先檢查圖表是否使用外部資料來源,然後取得其活頁簿路徑。
此範例檢查簡報第一張投影片的第一個圖形,若為連結外部活頁簿的圖表,則將get_ExternalWorkbookPath 印到主控台,接著儲存簡報的副本。
#include <DOM/Chart/ChartDataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"externalWorkbook.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto chartData = chart->get_ChartData();
if (chartData->get_DataSourceType() == ChartDataSourceType::ExternalWorkbook)
{
Console::WriteLine(chartData->get_ExternalWorkbookPath());
}
else
{
Console::WriteLine(u"The chart does not use an external workbook.");
}
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
presentation->Save(u"Result.pptx", Export::SaveFormat::Pptx);
編輯圖表資料
您可以以與編輯內部活頁簿相同的方式編輯外部活頁簿的資料。若外部活頁簿無法載入,會拋出例外。
此範例使用第一張投影片第一個圖形的圖表,且該圖表連結至可存取的外部活頁簿。它將第一系列第一資料點的儲存格值設為 100,並儲存更新後的簡報。編輯儲存格值會更新連結的外部 XLSX 檔案,若需保留原始活頁簿,請使用副本。
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto presentation = MakeObject<Presentation>(u"presentation.pptx");
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto series = chart->get_ChartData()->get_Series();
if (series->get_Count() > 0 && series->idx_get(0)->get_DataPoints()->get_Count() > 0)
{
auto valueCell = series->idx_get(0)->get_DataPoints()->idx_get(0)->get_Value()->get_AsCell();
if (valueCell != nullptr)
{
valueCell->set_Value(ObjectExt::Box<int32_t>(100));
presentation->Save(u"presentation_out.pptx", Export::SaveFormat::Pptx);
}
else
{
Console::WriteLine(u"The first data point is not linked to a workbook cell.");
}
}
else
{
Console::WriteLine(u"The chart has no data points to edit.");
}
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
從圖表快取還原活頁簿
如果圖表使用的外部活頁簿遺失或無法使用,Aspose.Slides 能從簡報中快取的資料重建圖表活頁簿。建立LoadOptions,使用set_SpreadsheetOptions 進行設定,並在開啟簡報前呼叫ISpreadsheetOptions::set_RecoverWorkbookFromChartCache 並傳入 true。
以下 C++ 範例從第一張投影片第一個圖形的圖表還原快取資料,該圖表參考無法使用的外部活頁簿。它透過IChart::get_ChartData 與IChartData::get_ChartDataWorkbook 取得還原的資料:
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <DOM/SpreadsheetOptions.h>
#include <system/console.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto spreadsheetOptions = MakeObject<SpreadsheetOptions>();
spreadsheetOptions->set_RecoverWorkbookFromChartCache(true);
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_SpreadsheetOptions(spreadsheetOptions);
auto presentation = MakeObject<Presentation>(u"presentation.pptx", loadOptions);
auto slide = presentation->get_Slide(0);
auto chart = slide->get_Shapes()->get_Count() > 0 ? AsCast<IChart>(slide->get_Shape(0)) : nullptr;
if (chart != nullptr)
{
auto recoveredWorkbook = chart->get_ChartData()->get_ChartDataWorkbook();
// 在此讀取或修改還原的活頁簿資料。
}
else
{
Console::WriteLine(u"The first shape is not a chart.");
}
若外部活頁簿無法使用且未啟用還原,Aspose.Slides 會拋出System::InvalidOperationException。僅在接受以快取圖表資料作為可接受的備援時才啟用還原,因為快取可能不包含外部活頁簿在簡報最後一次更新後所做的變更。
常見問題集
我可以判斷特定圖表是連結至外部活頁簿還是內嵌活頁簿嗎?
可以。圖表具有資料來源類型與外部活頁簿路徑,若來源為外部活頁簿,您即可讀取完整路徑以確認使用了外部檔案。
是否支援相對路徑的外部活頁簿,且它們如何儲存?
支援。若指定相對路徑,系統會自動轉換為絕對路徑。簡報會在 PPTX 檔案中儲存絕對路徑,搬移活頁簿時可能需要更新連結。
我可以使用位於網路資源/分享上的活頁簿嗎?
可以,此類活頁簿可作為外部資料來源使用。然而,Aspose.Slides 不支援直接編輯遠端活頁簿——它們只能作為來源使用。
Aspose.Slides 在儲存簡報時會覆寫外部 XLSX 嗎?
簡報會儲存指向外部檔案的連結。編輯以儲存格為基礎的圖表資料也可能更新連結的本機 XLSX 檔案。若原始檔案必須保持不變,請使用該活頁簿的副本。
如果外部檔案有密碼保護,該怎麼辦?
Aspose.Slides 連結時不接受密碼。常見做法是事先移除保護,或使用如Aspose.Cells 之類的工具產生已解密的副本,再連結至該副本。
多個圖表可以參考同一個外部活頁簿嗎?
可以。每個圖表都會儲存自己的連結。若它們皆指向同一檔案,更新該檔案後,下次載入資料時所有圖表皆會反映變更。

