使用 C++ 在演示文稿中管理图表工作簿
概述
本文解释了如何在 Aspose.Slides 中使用图表工作簿。它展示了如何通过工作簿流读取和写入图表数据,使用工作簿单元格作为图表数据标签,访问工作表集合,以及为图表值指定数据源类型。
它还涵盖了使用外部工作簿作为图表数据源的操作。示例演示了如何创建和分配外部工作簿,检索链接到图表的外部工作簿路径,以及在工作簿可用时编辑图表数据。
对于表示缺失数据的工作簿单元格,请参阅控制空单元格的显示了解空单元格与零的差异,以及可用显示模式的折线图比较。
从工作簿读取和写入图表数据
Aspose.Slides 提供了ReadWorkbookStream和WriteWorkbookStream方法,允许您读取和写入图表数据工作簿(其中包含使用 Aspose.Cells 编辑的图表数据)。注意图表数据必须以相同的方式组织,或具有与源相似的结构。
#include <DOM/Chart/Chart.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <system/io/memory_stream.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System::IO;
auto pres = System::MakeObject<Presentation>(u"chart.pptx");
auto chart = System::ExplicitCast<Chart>(pres->get_Slide(0)->get_Shape(0));
auto data = chart->get_ChartData();
auto = data->ReadWorkbookStream();
data->get_Series()->Clear();
data->get_Categories()->Clear();
stream->set_Position(0);
data->WriteWorkbookStream(stream);
在工作簿修改后验证图表布局
当您用修改后的工作簿替换嵌入的工作簿时,图表会保留其原始的系列和类别集合。这种不匹配可能导致IChart::ValidateChartLayout因索引超出范围错误而失败。在将更新的工作簿写回图表之前,请清除现有的系列和类别。
// 修改工作簿流后(例如,使用 Aspose.Cells)
auto updatedWorkbook = chartData->ReadWorkbookStream();
// 清除现有的数据引用。
chartData->get_Series()->Clear();
chartData->get_Categories()->Clear();
updatedWorkbook->set_Position(0);
chartData->WriteWorkbookStream(updatedWorkbook);
chart->ValidateChartLayout();
清除集合可确保图表数据结构与新工作簿保持一致,从而使ValidateChartLayout能够顺利完成而不报错。
将工作簿单元格设为图表数据标签
- 创建一个Presentation类的实例。
- 通过索引获取幻灯片的引用。
- 添加一个带有数据的气泡图。
- 访问图表系列。
- 将工作簿单元格设为数据标签。
- 保存演示文稿。
下面的 C++ 代码演示如何将工作簿单元格设为图表数据标签:
#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/ISlideCollection.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 Aspose::Slides::Export;
System::String lbl0 = u"Label 0 cell value";
System::String lbl1 = u"Label 1 cell value";
System::String lbl2 = u"Label 2 cell value";
// 实例化一个表示演示文稿文件的 Presentation 类
auto pres = System::MakeObject<Presentation>(u"chart2.pptx");
auto slide = pres->get_Slides()->idx_get(0);
auto chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Bubble, 50.0f, 50.0f, 600.0f, 400.0f, true);
auto series = chart->get_ChartData()->get_Series();
series->idx_get(0)->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLabelValueFromCell(true);
auto wb = chart->get_ChartData()->get_ChartDataWorkbook();
series->idx_get(0)->get_Labels()->idx_get(0)->set_ValueFromCell(wb->GetCell(0, u"A10", System::ObjectExt::Box<System::String>(lbl0)));
series->idx_get(0)->get_Labels()->idx_get(1)->set_ValueFromCell(wb->GetCell(0, u"A11", System::ObjectExt::Box<System::String>(lbl1)));
series->idx_get(0)->get_Labels()->idx_get(2)->set_ValueFromCell(wb->GetCell(0, u"A12", System::ObjectExt::Box<System::String>(lbl2)));
pres->Save(u"resultchart.pptx", SaveFormat::Pptx);
管理工作表
下面的 C++ 代码演示了使用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/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/enumerator_adapter.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace System;
auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50.0f, 50.0f, 400.0f, 500.0f);
auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto worksheets = workbook->get_Worksheets();
for (auto ws : System::IterateOver(worksheets))
System::Console::WriteLine(ws->get_Name());
指定数据源类型
下面的 C++ 代码演示如何为数据源指定类型:
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/DataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.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/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
auto pres = System::MakeObject<Presentation>();
auto chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Column3D, 50.0f, 50.0f, 600.0f, 400.0f, true);
auto chartData = chart->get_ChartData();
auto val = chart->get_ChartData()->get_Series()->idx_get(0)->get_Name();
val->set_DataSourceType(DataSourceType::StringLiterals);
val->set_Data(System::ObjectExt::Box<System::String>(u"LiteralString"));
val = chartData->get_Series()->idx_get(1)->get_Name();
val->set_Data(chartData->get_ChartDataWorkbook()->GetCell(0, u"B1", System::ObjectExt::Box<System::String>(u"NewCell")));
pres->Save(u"pres.pptx", SaveFormat::Pptx);
检测不受支持的嵌入式工作簿格式
Aspose.Slides 不支持可以嵌入某些图表的 Excel 二进制工作簿(.xlsb)格式。您可以在IChartData上使用get_EmbeddedWorkbookType方法,并结合WorkbookType枚举来检测不受支持的格式并跳过这些图表。
#include <DOM/Chart/ChartDataSourceType.h>
#include <DOM/Chart/WorkbookType.h>
#include <DOM/IChart.h>
#include <DOM/ISlide.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/Presentation.h>
#include <system/enumerator_adapter.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
auto presentation = System::MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
for (auto&& shape : System::IterateOver(slide->get_Shapes()))
{
if (!System::ObjectExt::Is<IChart>(shape))
{
continue;
}
auto chart = System::ExplicitCast<IChart>(shape);
auto chartData = chart->get_ChartData();
if (chartData->get_DataSourceType() == ChartDataSourceType::InternalWorkbook &&
chartData->get_EmbeddedWorkbookType() == WorkbookType::WorkbookBinaryMacro)
{
// 嵌入的工作簿为 .xlsb 格式,不受支持。
continue;
}
// 在此读取或修改图表工作簿数据。
}
外部工作簿
Aspose.Slides 支持将外部工作簿用作图表的数据源。
创建外部工作簿
使用**ReadWorkbookStream**和**SetExternalWorkbook**方法,您可以从头创建外部工作簿,或将内部工作簿设为外部。
下面的 C++ 代码演示外部工作簿的创建过程:
#include <DOM/Chart/ChartType.h>
#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 <Export/SaveFormat.h>
#include <system/io/file_mode.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 Aspose::Slides::Export;
using namespace System::IO;
auto pres = System::MakeObject<Presentation>();
const System::String workbookPath = u"externalWorkbook1.xlsx";
auto chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Pie, 50.0f, 50.0f, 400.0f, 600.0f);
auto chartData = chart->get_ChartData();
{
System::SharedPtr<System::IO::FileStream> fileStream = System::MakeObject<System::IO::FileStream>(workbookPath, System::IO::FileMode::Create);
System::ArrayPtr<uint8_t> workbookData = chartData->ReadWorkbookStream()->ToArray();
fileStream->Write(workbookData, 0, workbookData->get_Length());
}
chartData->SetExternalWorkbook(System::IO::Path::GetFullPath(workbookPath));
pres->Save(u"externalWorkbook.pptx", SaveFormat::Pptx);
设置外部工作簿
使用**IChartData::SetExternalWorkbook**方法,您可以将外部工作簿分配给图表作为其数据源。该方法还可用于更新外部工作簿的路径(如果该工作簿已移动)。
虽然无法编辑存储在远程位置或资源中的工作簿数据,但仍可将此类工作簿用作外部数据源。如果提供了外部工作簿的相对路径,它将自动转换为完整路径。
下面的 C++ 代码演示如何设置外部工作簿:
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.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 Aspose::Slides::Export;
using namespace System::IO;
auto pres = System::MakeObject<Presentation>();
auto chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Pie, 50.0f, 50.0f, 400.0f, 600.0f, false);
auto chartData = chart->get_ChartData();
chartData->SetExternalWorkbook(System::IO::Path::GetFullPath(u"externalWorkbook.xlsx"));
chartData->get_Series()->Add(chartData->get_ChartDataWorkbook()->GetCell(0, u"B1"), ChartType::Pie);
auto dataPoints = chartData->get_Series()->idx_get(0)->get_DataPoints();
auto workbook = chartData->get_ChartDataWorkbook();
dataPoints->AddDataPointForPieSeries(workbook->GetCell(0, u"B2"));
dataPoints->AddDataPointForPieSeries(workbook->GetCell(0, u"B3"));
dataPoints->AddDataPointForPieSeries(workbook->GetCell(0, u"B4"));
auto categories = chartData->get_Categories();
categories->Add(workbook->GetCell(0, u"A2"));
categories->Add(workbook->GetCell(0, u"A3"));
categories->Add(workbook->GetCell(0, u"A4"));
pres->Save(u"Presentation_with_externalWorkbook.pptx", SaveFormat::Pptx);
updateChartData 参数(位于 SetExternalWorkbook 方法下)用于指定是否加载 Excel 工作簿。
- 当
updateChartData的值设为false时,仅更新工作簿路径——图表数据不会从目标工作簿加载或更新。当目标工作簿不存在或不可用时,您可能需要使用此设置。 - 当
updateChartData的值设为true时,图表数据将从目标工作簿更新。
#include <DOM/Chart/ChartData.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
auto chart = slide->get_Shapes()->AddChart(ChartType::Pie, 50.0f, 50.0f, 400.0f, 600.0f, true);
System::SharedPtr<IChartData> chartData = chart->get_ChartData();
System::SharedPtr<ChartData> concreteChartData = System::AsCast<ChartData>(chartData);
concreteChartData->SetExternalWorkbook(u"http://path/doesnt/exists", false);
pres->Save(u"SetExternalWorkbookWithUpdateChartData.pptx", SaveFormat::Pptx);
获取图表的外部数据源工作簿路径
- 创建一个Presentation类的实例。
- 通过索引获取幻灯片的引用。
- 为图表形状创建对象。
- 为表示图表数据源的源(
ChartDataSourceType)类型创建对象。 - 根据源类型与外部工作簿数据源类型相同的情况指定相应的条件。
下面的 C++ 代码演示此操作:
#include <DOM/Chart/ChartDataSourceType.h>
#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 <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
auto pres = System::MakeObject<Presentation>(u"pres.pptx");
auto slide = pres->get_Slides()->idx_get(1);
auto chart = System::ExplicitCast<IChart>(slide->get_Shapes()->idx_get(0));
ChartDataSourceType sourceType = chart->get_ChartData()->get_DataSourceType();
if (sourceType == ChartDataSourceType::ExternalWorkbook)
{
System::String path = chart->get_ChartData()->get_ExternalWorkbookPath();
}
// Saves the presentation
pres->Save(u"Result.pptx", SaveFormat::Pptx);
编辑图表数据
您可以像修改内部工作簿内容一样编辑外部工作簿中的数据。当外部工作簿无法加载时,会抛出异常。
下面的 C++ 代码实现了上述过程:
#include <DOM/Chart/Chart.h>
#include <DOM/Chart/ChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartDataPointCollection.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/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
const String templatePath = u"../templates/presentation.pptx";
const String outPath = u"../out/presentation-out.pptx";
System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(templatePath);
System::SharedPtr<Aspose::Slides::Charts::IChart> chart = System::AsCast<Aspose::Slides::Charts::IChart>(pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0));
System::SharedPtr<Aspose::Slides::Charts::ChartData> chartData = System::ExplicitCast<Aspose::Slides::Charts::ChartData>(chart->get_ChartData());
chartData->get_Series()->idx_get(0)->get_DataPoints()->idx_get(0)->get_Value()->get_AsCell()->set_Value(System::ObjectExt::Box<int32_t>(100));
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
从图表缓存恢复工作簿
如果图表使用的外部工作簿缺失或不可用,Aspose.Slides 可以从演示文稿中缓存的数据重建图表工作簿。创建LoadOptions,使用set_SpreadsheetOptions进行配置,并在打开演示文稿前调用ISpreadsheetOptions::set_RecoverWorkbookFromChartCache并传入 true。
下面的 C++ 示例打开一个图表引用不可用外部工作簿的演示文稿,并通过IChart::get_ChartData和IChartData::get_ChartDataWorkbook访问恢复的数据:
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 shape = presentation->get_Slide(0)->get_Shape(0);
auto chart = System::ExplicitCast<IChart>(shape);
auto recoveredWorkbook = chart->get_ChartData()->get_ChartDataWorkbook();
// Read or modify the recovered workbook data here.
presentation->Dispose();
如果外部工作簿不可用且未启用恢复,Aspose.Slides 将抛出 System::InvalidOperationException。仅当使用缓存的图表数据是可接受的后备方案时才启用恢复,因为缓存可能不包含演示文稿上次更新后对外部工作簿所做的更改。
常见问题
我能确定特定图表是链接到外部工作簿还是嵌入式工作簿吗?
是的。图表具有数据源类型和外部工作簿路径;如果源是外部工作簿,您可以读取完整路径以确保正在使用外部文件。
是否支持外部工作簿的相对路径,且它们如何存储?
是的。如果您指定相对路径,系统会自动将其转换为绝对路径。这对项目的可移植性很方便;但请注意,演示文稿会在 PPTX 文件中存储绝对路径。
我可以使用位于网络资源/共享上的工作簿吗?
是的,这类工作簿可以用作外部数据源。但是,不支持直接从 Aspose.Slides 编辑远程工作簿——它们只能用作数据源。
Aspose.Slides 在保存演示文稿时会覆盖外部 XLSX 吗?
不会。演示文稿存储外部文件的链接并使用它读取数据。保存演示文稿时不会修改外部文件本身。
如果外部文件受密码保护,我该怎么办?
Aspose.Slides 在链接时不接受密码。常见做法是事先去除保护或准备一个已解密的副本(例如,使用Aspose.Cells),然后链接到该副本。
多个图表可以引用同一个外部工作簿吗?
是的。每个图表都存储自己的链接。如果它们都指向同一个文件,在下次加载数据时,文件的更新将反映在每个图表中。