表格是组织和呈现数据的强大工具。它将数据排列成行和列,使作者更容易阐述不同数据类别之间的关系,也便于读者理解和分析复杂的数据。在本文中,您将学习如何使用 Spire.Doc for C++ 以编程方式在 Word 文档中创建表格。
安装 Spire.Doc for C++
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
在 Word 中创建表格
Spire.Doc for C++ 提供了 Section->AddTable() 方法来将表格添加到 Word 文档的某个节。详细步骤如下:
- 初始化 Document 类的一个实例。
- 使用 Document->AddSection() 方法向文档添加一个节。
- 定义表头行和剩余行的数据,分别存储在一维向量和二维向量中。
- 使用 Section->AddTable() 方法将表格添加到该节。
- 使用 Table->ResetCells(int, int) 方法指定表格中的行数和列数。
- 将一维向量中的数据添加到标题行并设置格式。
- 将二维向量中的数据添加到剩余的行并设置格式。
- 使用 Document->SaveToFile() 方法保存结果文档。
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
	//初始化 Document 类的一个实例
	intrusive_ptr<Document> doc = new Document();
	//在文档中添加一个节
	intrusive_ptr<Section> section = doc->AddSection();
	//设置节的页边距
	section->GetPageSetup()->GetMargins()->SetAll(72);
	//定义标题行的数据
	vector<wstring> header = { L"国家", L"首都", L"大陆", L"国土面积", L"人口数量" };
	//定义剩余行的数据
	vector<vector<wstring>> data =
	{
		{L"阿根廷", L"布宜诺斯艾利斯", L"南美", L"2777815", L"32300003"},
		{L"玻利维亚", L"拉巴斯", L"南美", L"1098575", L"7300000"},
		{L"巴西", L"巴西利亚", L"南美", L"8511196", L"150400000"},
		{L"加拿大", L"渥太华", L"北美", L"9976147", L"26500000"},
		{L"智利", L"圣地亚哥", L"南美", L"756943", L"13200000"},
		{L"哥伦比亚", L"波哥大", L"南美", L"1138907", L"33000000"},
		{L"古巴", L"哈瓦那", L"北美", L"114524", L"10600000"},
		{L"厄瓜多尔", L"基多", L"南美", L"455502", L"10600000"},
		{L"萨尔瓦多", L"圣萨尔瓦多", L"北美", L"20865", L"5300000"},
		{L"圭亚那", L"乔治城", L"南美", L"214969", L"800000"},
		{L"牙买加", L"金士顿", L"北美", L"11424", L"2500000"},
		{L"墨西哥", L"墨西哥城", L"北美", L"1967180", L"88600000"},
		{L"尼加拉瓜", L"马那瓜", L"北美", L"139000", L"3900000"},
		{L"巴拉圭", L"亚松森", L"南美", L"406576", L"4660000"},
		{L"秘鲁", L"利马", L"南美", L"1285215", L"21600000"},
		{L"美国", L"华盛顿", L"北美", L"9363130", L"249200000"},
		{L"乌拉圭", L"蒙得维的亚", L"南美", L"176140", L"3002000"},
		{L"委内瑞拉", L"加拉加斯", L"南美", L"912047", L"19700000"}
	};
	//向该部节添加表格
	intrusive_ptr<Table> table = section->AddTable(true);
	//指定表格的行数和列数
	table->ResetCells(data.size() + 1, header.size());
	//将第一行设置为标题行
	intrusive_ptr<TableRow> row = table->GetRows()->GetItemInRowCollection(0);
	row->SetIsHeader(true);
	//设置标题行的高度和背景颜色
	row->SetHeight(20);
	row->SetHeightType(TableRowHeightType::Exactly);
	for (int i = 0; i < row->GetCells()->GetCount(); i++)
	{
		row->GetCells()->GetItemInCellCollection(i)->GetCellFormat()->GetShading()->SetBackgroundPatternColor(Color::FromArgb(142, 170, 219));
	}
	//将数据添加到标题行并设置格式
	for (size_t i = 0; i < header.size(); i++)
	{
		//添加一个段落
		intrusive_ptr<Paragraph> p1 = row->GetCells()->GetItemInCellCollection(i)->AddParagraph();
		//设置对齐方式
		p1->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
		row->GetCells()->GetItemInCellCollection(i)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
		//添加数据
		intrusive_ptr<TextRange> tR1 = p1->AppendText(header[i].c_str());
		//设置数据格式
		tR1->GetCharacterFormat()->SetFontName(L"宋体");
		tR1->GetCharacterFormat()->SetFontSize(12);
		tR1->GetCharacterFormat()->SetBold(true);
	}
	//向剩余行添加数据并设置格式
	for (size_t r = 0; r < data.size(); r++)
	{
		//设置剩余行的高度
		intrusive_ptr<TableRow> dataRow = table->GetRows()->GetItemInRowCollection(r + 1);
		dataRow->SetHeight(20);
		dataRow->SetHeightType(TableRowHeightType::Exactly);
		for (size_t c = 0; c < data[r].size(); c++)
		{
			//添加一个段落
			intrusive_ptr<Paragraph> p2 = dataRow->GetCells()->GetItemInCellCollection(c)->AddParagraph();
			//设置对齐方式
			dataRow->GetCells()->GetItemInCellCollection(c)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
			//添加数据
			intrusive_ptr<TextRange> tR2 = p2->AppendText(data[r][c].c_str());
			//设置数据格式
			tR2->GetCharacterFormat()->SetFontName(L"宋体");
			tR2->GetCharacterFormat()->SetFontSize(11);
		}
	}
	//保存结果文档
	doc->SaveToFile(L"创建表格.docx", FileFormat::Docx2013);
	doc->Close();
}
在 Word 中创建嵌套表格
Spire.Doc for C++ 提供了 TableCell->AddTable() 方法来将嵌套表格添加到特定的表格单元格。详细步骤如下:
- 初始化 Document 类的一个实例。
- 使用 Document->AddSection() 方法向文档添加一个节。
- 使用 Section.AddTable() 方法将表格添加到该节。
- 使用 Table->ResetCells(int, int) 方法指定表格中的行数和列数。
- 获取表格的行并将数据添加到每行的单元格中。
- 使用 TableCell->AddTable() 方法将嵌套表格添加到特定表格单元格。
- 指定嵌套表格中的行数和列数。
- 获取嵌套表格的行并将数据添加到每行的单元格中。
- 使用 Document->SaveToFile() 方法保存结果文档。
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
	//初始化 Document 类的一个实例
	intrusive_ptr<Document> doc = new Document();
	//在文档中添加一个节
	intrusive_ptr<Section> section = doc->AddSection();
	//设置节的页边距
	section->GetPageSetup()->GetMargins()->SetAll(72);
	//向节中添加表格
	intrusive_ptr<Table> table = section->AddTable(true);
	//设置表中的行数和列数
	table->ResetCells(2, 2);
	//根据窗口自动调整表格宽度
	table->AutoFit(AutoFitBehaviorType::AutoFitToWindow);
	//获取表行
	intrusive_ptr<TableRow> row1 = table->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> row2 = table->GetRows()->GetItemInRowCollection(1);
	//将数据添加到表的单元格
	intrusive_ptr<TableCell> cell1 = row1->GetCells()->GetItemInCellCollection(0);
	intrusive_ptr<TextRange> tR = cell1->AddParagraph()->AppendText(L"产品");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell2 = row1->GetCells()->GetItemInCellCollection(1);
	tR = cell2->AddParagraph()->AppendText(L"产品说明");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell3 = row2->GetCells()->GetItemInCellCollection(0);
	cell3->AddParagraph()->AppendText(L"Spire.Doc for C++");
	intrusive_ptr<TableCell> cell4 = row2->GetCells()->GetItemInCellCollection(1);
	cell4->AddParagraph()->AppendText(L"Spire.Doc for C++ 是一款专门对 Word 文档进行操作的 C++ 类库。"
		L"这款控件具有快速和高质量的性能,"
		L"主要功能在于帮助开发人员轻松快捷高效地"
		L"创建、编辑、转换、及比较Microsoft Word 文档。");
	//将嵌套表添加到第四个单元格
	intrusive_ptr<Table> nestedTable = cell4->AddTable(true);
	//Set the number of rows and columns in the nested table
	nestedTable->ResetCells(3, 2);
	//根据内容自动调整表格宽度
	nestedTable->AutoFit(AutoFitBehaviorType::AutoFitToContents);
	//获取表行
	intrusive_ptr<TableRow> nestedRow1 = nestedTable->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> nestedRow2 = nestedTable->GetRows()->GetItemInRowCollection(1);
	intrusive_ptr<TableRow> nestedRow3 = nestedTable->GetRows()->GetItemInRowCollection(2);
	//将数据添加到嵌套表的单元格
	intrusive_ptr<TableCell> nestedCell1 = nestedRow1->GetCells()->GetItemInCellCollection(0);
	tR = nestedCell1->AddParagraph()->AppendText(L"商品");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell2 = nestedRow1->GetCells()->GetItemInCellCollection(1);
	tR = nestedCell2->AddParagraph()->AppendText(L"价格");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell3 = nestedRow2->GetCells()->GetItemInCellCollection(0);
	nestedCell3->AddParagraph()->AppendText(L"Developer Subscription");
	intrusive_ptr<TableCell> nestedCell4 = nestedRow2->GetCells()->GetItemInCellCollection(1);
	nestedCell4->AddParagraph()->AppendText(L"$999");
	intrusive_ptr<TableCell> nestedCell5 = nestedRow3->GetCells()->GetItemInCellCollection(0);
	nestedCell5->AddParagraph()->AppendText(L"Developer OEM Subscription");
	intrusive_ptr<TableCell> nestedCell6 = nestedRow3->GetCells()->GetItemInCellCollection(1);
	nestedCell6->AddParagraph()->AppendText(L"$2999");
	//保存结果文档
	doc->SaveToFile(L"创建嵌套表格.docx", FileFormat::Docx2013);
	doc->Close();
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



