使用 C++ 在 PowerPoint 中創建和操作表格

Microsoft PowerPoint 提供了在 PowerPoint 演示文稿中插入表格的功能。表格允許您以行和列的形式排列數據。此外,他們組織數據並使其易於查看和分析。為此,本文將教您如何使用 C++ 在 PowerPoint 演示文稿中創建和操作表格。

用於在 PowerPoint 演示文稿中創建和操作表格的 C++ API

我們將使用 Aspose.Slides for C++ API 在 PowerPoint 演示文稿中創建和操作表格。它是一個功能強大且功能豐富的 API,無需安裝 Mircosoft PowerPoint 即可支持創建、讀取和修改 PowerPoint 文件。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Slides.Cpp

使用 C++ 在 PowerPoint 演示文稿中創建表格

以下是在 PowerPoint 演示文稿中創建表格的步驟。

以下示例代碼演示如何使用 C++ 在 PowerPoint 演示文稿中創建表格。

// 文件路徑
const String outputFilePath = u"OutputDirectory\\CreateTable_out.pptx";

// 創建 Presentation 類的實例
auto presentation = System::MakeObject<Presentation>();

// 訪問第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 定義具有寬度的列和具有高度的行
System::ArrayPtr<double> dblCols = System::MakeObject<System::Array<double>>(4, 70);
System::ArrayPtr<double> dblRows = System::MakeObject<System::Array<double>>(4, 70);

// 將表格形狀添加到幻燈片
SharedPtr<ITable> table = slide->get_Shapes()->AddTable(100, 50, dblCols, dblRows);

// 為每個單元格設置邊框格式
for (int x = 0; x < table->get_Rows()->get_Count(); x++)
{
	SharedPtr<IRow> row = table->get_Rows()->idx_get(x);
	for (int y = 0; y < row->get_Count(); y++)
	{
		SharedPtr<ICell> cell = row->idx_get(y);

		cell->get_CellFormat()->get_BorderTop()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_CellFormat()->get_BorderTop()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_CellFormat()->get_BorderTop()->set_Width(5);

		cell->get_CellFormat()->get_BorderBottom()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_CellFormat()->get_BorderBottom()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_CellFormat()->get_BorderBottom()->set_Width(5);

		cell->get_CellFormat()->get_BorderLeft()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_CellFormat()->get_BorderLeft()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_CellFormat()->get_BorderLeft()->set_Width(5);

		cell->get_CellFormat()->get_BorderRight()->get_FillFormat()->set_FillType(FillType::Solid);
		cell->get_CellFormat()->get_BorderRight()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
		cell->get_CellFormat()->get_BorderRight()->set_Width(5);
	}
}

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
示例代碼生成的表格

示例代碼生成的表格

使用 C++ 訪問和修改 PowerPoint 演示文稿中的表格

您還可以訪問和修改 PowerPoint 演示文稿中的現有表格。以下是訪問和修改 PowerPoint 演示文稿中的表格的步驟。

以下示例代碼顯示如何使用 C++ 訪問和修改 PowerPoint 演示文稿中的表格。

// 文件路徑
const String sourceFilePath = u"OutputDirectory\\CreateTable_out.pptx";
const String outputFilePath = u"OutputDirectory\\AccessTable_out.pptx";

// 加載演示文稿文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 訪問第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 訪問表
SharedPtr<ITable> table;

for (SharedPtr<IShape> shape : slide->get_Shapes())
{
	if (System::ObjectExt::Is<ITable>(shape)) {
		table = System::DynamicCast_noexcept<ITable>(shape);
	}
}

// 設置文字
table->idx_get(0, 1)->get_TextFrame()->set_Text(u"Aspose");

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

使用 C++ 在 PowerPoint 表格中設置文本方向

以下是在 PowerPoint 表格中設置文本方向的步驟。

以下示例代碼顯示如何使用 C++ 設置 PowerPoint 表格中的文本方向。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\Slides\\PresentationWithTable.pptx";
const String outputFilePath = u"OutputDirectory\\SetTextDirectionInTable_out.pptx";

// 加載演示文稿文件
auto presentation = System::MakeObject<Presentation>(sourceFilePath);

// 訪問第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 訪問表
SharedPtr<ITable> table;

for (SharedPtr<IShape> shape : slide->get_Shapes())
{
	if (System::ObjectExt::Is<ITable>(shape)) {
		table = System::DynamicCast_noexcept<ITable>(shape);
	}
}

// 設置文字方向
SharedPtr<ICell> cell = table->idx_get(0, 1);
cell->set_TextAnchorType(TextAnchorType::Center);
cell->set_TextVerticalType(TextVerticalType::Vertical270);

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
示例代碼生成的輸出圖像

示例代碼生成的輸出圖像

獲得免費許可證

為了在沒有評估限制的情況下試用 API,您可以申請免費的臨時許可證

結論

在本文中,您了解瞭如何在 PowerPoint 演示文稿中創建和更新表格。此外,您還了解瞭如何使用 Aspose.Slides for C++ API 設置 PowerPoint 表格中文本的方向。它是一個強大的 API,提供了一系列用於處理 PowerPoint 文件的附加功能。您可以通過訪問 官方文檔 來詳細探索 API。如有任何疑問,請隨時通過我們的免費支持論壇與我們聯繫。

也可以看看