Word处理控件Aspose.Words功能演示:使用 C++ 在 Word 文档 (DOC/DOCX) 中插入表格

news2024/9/30 15:28:13

Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(761297826)icon-default.png?t=N176https://www.evget.com/product/4116/download

表格有助于组织信息和图表。我们经常在word文档( DOCX / DOC )中插入表格来展示信息。在文字处理应用程序中,您可以使用 C++ 轻松创建表格。您可以通过以下示例来学习在 Word 文档中使用表格:

一、在 Word 文档 API 中插入表格

首先,请注意您将使用Aspose.Words for C++ API 在 word 文档中插入表格。您可以通过从新版本或通过NuGet库下载它来配置 API 。正确配置后,您可以简单地利用 API 公开的方法、属性和类,以便可以使用一些简单的 API 调用来创建、编辑或操作 Microsoft Word 文档,如 DOCX 或 DOC 文件。

二、使用 C++ 在 Word 文档中插入表格

您可以通过几个简单的步骤在 Word 文档中插入表格。不过这里需要注意的是,必须将文档对象传递给每个节点的构造函数,这样所有的子节点都属于同一个对象。您需要按照下面列出的步骤操作:

  1. 初始化文档类的对象
  2. 创建表对象
  3. 将表添加到文档
  4. 创建行和列
  5. 在表格单元格上应用自动调整
  6. 保存输出 Word 文档

下面的代码片段显示了如何使用 C++ 在 Word 文档 (DOCX/DOC) 中插入表格:

// The path to the documents directory.
System::String outputDataDir = dataDir;
System::SharedPtr<Document> doc = System::MakeObject<Document>();

// We start by creating the table object. Note how we must pass the document object
// To the constructor of each node. This is because every node we create must belong
// To some document.
System::SharedPtr<Table> table = System::MakeObject<Table>(doc);

// Add the table to the document.
doc->get_FirstSection()->get_Body()->AppendChild(table);

// Here we could call EnsureMinimum to create the rows and cells for us. This method is used
// To ensure that the specified node is valid, in this case a valid table should have at least one
// Row and one cell, therefore this method creates them for us.
// Instead we will handle creating the row and table ourselves. This would be the best way to do this
// If we were creating a table inside an algorthim for example.
System::SharedPtr<Row> row = System::MakeObject<Row>(doc);
row->get_RowFormat()->set_AllowBreakAcrossPages(true);
table->AppendChild(row);

// We can now apply any auto fit settings.
table->AutoFit(AutoFitBehavior::FixedColumnWidths);

// Create a cell and add it to the row
System::SharedPtr<Cell> cell = System::MakeObject<Cell>(doc);
cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());
cell->get_CellFormat()->set_Width(80);

// Add a paragraph to the cell as well as a new run with some text.
cell->AppendChild(System::MakeObject<Paragraph>(doc));
cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text"));

// Add the cell to the row.
row->AppendChild(cell);

// We would then repeat the process for the other cells and rows in the table.
// We can also speed things up by cloning existing cells and rows.
row->AppendChild((System::StaticCast<Node>(cell))->Clone(false));
row->get_LastCell()->AppendChild(System::MakeObject<Paragraph>(doc));
row->get_LastCell()->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 2 Text"));
System::String outputPath = outputDataDir + u"InsertTableDirectly.doc";

// Save the document to disk.
doc->Save(outputPath);

三、使用 C++ 在 Word 文档中从 HTML 插入表格

HTML 文件可能包含表格,您需要将其插入到 DOCX、DOC 等 word 文档中。或者您可能需要从网站复制表格。因此,无需从头开始创建和设计表格,您可以轻松地将 HTML 标记作为表格解析到 Word 文档中。例如,您可以使用以下 HTML 字符串将表格添加到 word 文档中:

<table><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr></table>
我们使内容保持简单,以便可以通过基本但重要的用例来演示对表格标签的支持。此外,这里需要注意的是,AutoFit 不能应用于从 HTML 创建的表格。

让我们按照以下步骤在 Word 文档中插入 HTML 表格:

  1. 初始化文档类的实例
  2. 使用InsertHtml方法传递 HTML 标记
  3. 保存输出DOCX word文件

下面的代码遵循这些步骤,并展示了如何使用 C++ 在 HTML 的 Word 文档中创建表格:

// The path to the documents directory.
System::String outputDataDir = dataDir;
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// Insert the table from HTML. Note that AutoFitSettings does not apply to tables
// Inserted from HTML.
builder->InsertHtml(u"<table><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr></table>");

System::String outputPath = outputDataDir + u"InsertTableFromHtml.doc";
// Save the document to disk.
doc->Save(outputPath);

您会注意到此方法比我们上面探讨的方法要简单一些。原因是,您不需要为行、列或单元格逐个添加每个节点,因为 HTML 字符串中的 Table 标记包含所有信息。以下是添加到 Word 文档中的这个简单 HTML 表格的屏幕截图:

四、在 C++ 中使用文档生成器插入表

Aspose.Words for C++ API 的最佳之处在于它提供了多种功能,这些功能成为 API 的竞争优势,并使其在其他选项中脱颖而出。同样,使用文档生成器插入表格的功能是在 word 文档(DOC/DOCX)中添加表格的另一种方法。因此,让我们从三个不同的角度来探讨细节:

1) 使用 C++ 使用文档生成器在 DOCX 中插入简单表格

要使用文档生成器在 word 文档中添加一个简单的表格,您需要按照以下步骤操作:

  1. 创建文档对象
  2. 调用StartTable()方法并插入单元格
  3. 添加行和单元格
  4. 保存输出 DOCX 文件

此外,下面的代码片段显示了如何使用 C++ 在 DOCX 文件中插入简单表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// We call this method to start building the table.
builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, Cell 1 Content.");

// Build the second cell
builder->InsertCell();
builder->Write(u"Row 1, Cell 2 Content.");

// Call the following method to end the row and start a new row.
builder->EndRow();

// Build the first cell of the second row.
builder->InsertCell();
builder->Write(u"Row 2, Cell 1 Content");

// Build the second cell.
builder->InsertCell();
builder->Write(u"Row 2, Cell 2 Content.");
builder->EndRow();

// Signal that we have finished building the table.
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.SimpleTable.doc";

// Save the document to disk.
doc->Save(outputPath);

2) 使用 C++ 使用文档生成器在 DOCX 中插入格式化表格

您可以使用以下步骤将格式化表格插入到 word 文档中:

  1. 初始化文档类的实例
  2. 制作标题行
  3. 为格式设置缩进和功能
  4. 重置字体格式
  5. 保存输出 Word DOCX 文件

下面的代码片段使用 C++ 在 DOCX 文件中创建格式化表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Table> table = builder->StartTable();

// Make the header row.
builder->InsertCell();

// Set the left indent for the table. Table wide formatting must be applied after
// At least one row is present in the table.
table->set_LeftIndent(20.0);

// Set height and define the height rule for the header row.
builder->get_RowFormat()->set_Height(40.0);
builder->get_RowFormat()->set_HeightRule(HeightRule::AtLeast);

// Some special features for the header row.
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::FromArgb(198, 217, 241));
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_Font()->set_Size(16);
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
builder->get_CellFormat()->set_Width(100.0);
builder->Write(u"Header Row,\n Cell 1");

// We don't need to specify the width of this cell because it's inherited from the previous cell.
builder->InsertCell();
builder->Write(u"Header Row,\n Cell 2");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Header Row,\n Cell 3");
builder->EndRow();

// Set features for the other rows and cells.
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_White());
builder->get_CellFormat()->set_Width(100.0);
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center);

// Reset height and define a different height rule for table body
builder->get_RowFormat()->set_Height(30.0);
builder->get_RowFormat()->set_HeightRule(HeightRule::Auto);
builder->InsertCell();

// Reset font formatting.
builder->get_Font()->set_Size(12);
builder->get_Font()->set_Bold(false);

// Build the other cells.
builder->Write(u"Row 1, Cell 1 Content");
builder->InsertCell();
builder->Write(u"Row 1, Cell 2 Content");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Row 1, Cell 3 Content");
builder->EndRow();
builder->InsertCell();
builder->get_CellFormat()->set_Width(100.0);
builder->Write(u"Row 2, Cell 1 Content");
builder->InsertCell();
builder->Write(u"Row 2, Cell 2 Content");
builder->InsertCell();
builder->get_CellFormat()->set_Width(200.0);
builder->Write(u"Row 2, Cell 3 Content.");
builder->EndRow();
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.FormattedTable.doc";

// Save the document to disk.
doc->Save(outputPath);

3) 使用 C++ 使用文档生成器在 DOCX 中插入嵌套表

有时我们需要在现有表中添加另一个表。例如,表的某行或某列中的单元格可以包含子类别或某个其他字段的子表。在这种情况下,嵌套表很有用,可以按照以下步骤添加:

  1. 构建外部表,然后调用EndTable方法
  2. 在外表的单元格内构建内表
  3. 保存输出word文档

以下代码片段显示了如何使用 C++ 在 Word 文档中插入嵌套表格:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// Build the outer table.
System::SharedPtr<Cell> cell = builder->InsertCell();
builder->Writeln(u"Outer Table Cell 1");
builder->InsertCell();
builder->Writeln(u"Outer Table Cell 2");

// This call is important in order to create a nested table within the first table
// Without this call the cells inserted below will be appended to the outer table.
builder->EndTable();

// Move to the first cell of the outer table.
builder->MoveTo(cell->get_FirstParagraph());

// Build the inner table.
builder->InsertCell();
builder->Writeln(u"Inner Table Cell 1");
builder->InsertCell();
builder->Writeln(u"Inner Table Cell 2");
builder->EndTable();
System::String outputPath = outputDataDir + u"InsertTableUsingDocumentBuilder.NestedTable.doc";

// Save the document to disk.
doc->Save(outputPath);

以上便是如何使用 C++ 在 Word 文档 (DOC/DOCX) 中插入表格文件详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/380932.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于python的多线程数据库数据录入

说明&#xff1a; 使用python编程结合多线程技术&#xff0c;将已经在python文件中的数据批量写入到数据库&#xff0c;便于数据关系结构化管理。 环境配置&#xff1a; certifi2019.6.16 chardet3.0.4 idna2.8 PyMySQL0.9.3 requests2.22.0 urllib31.25.3 将所需要的环境保…

vue模板语法和数据绑定和el、data的两种

vue模板语法有两大类&#xff1a; 1.插值语法&#xff1a; 功能&#xff1a;用于解拆标签体内容 写法&#xff1a;{{xxx}}&#xff0c;xxx是js表达式&#xff0c;且可以直接读取到data中的所有属性 2.指令语法&#xff1a; 功能&#xff1a;用于解拆标签&#xff08;包括&…

《商用密码应用与安全性评估》第一章密码基础知识1.1应用概念

密码的概念与作用 概念 密码&#xff1a;采用特定变换的方法对信息进行加密保护、安全认证的技术、产品和服务。 密码技术&#xff1a;密码编码、实现、协议、安全防护、分析破译、以及密钥产生、分发、传递、使 用、销毁等技术。 密码技术核心&#xff1a;密码算法…

家用洗地机什么品牌质量好耐用?最适合家用的洗地机

近些年&#xff0c;随着消费水平的不断升级&#xff0c;我们对家电产品的要求也在逐步提高&#xff0c;就以这几年非常流行的洗地机为例&#xff0c;如今的人们在选洗地机时&#xff0c;会综合考虑价位、技术、配置、颜值、功能等多个方面&#xff0c;那么市场上家用洗地机什么…

JAVACC

JavaCC全称为Java Compiler Compiler&#xff0c;它是一个生成器&#xff0c;用于生成词法分析器&#xff08;lexical analysers&#xff09;和语法分析器&#xff08;parsers&#xff09;&#xff1b;JavaCC本身并不是词法分析器和语法分析器&#xff0c;它是一个生成器&#…

mysql数据库之索引结构

MySQL的索引是在存储引擎层实现的&#xff0c;不同的存储引擎有不同的结构。 一、常见索引。 索引结构描述BTree索引最常见的索引类型&#xff0c;大部分引擎都支持B树索引Hash索引底层数据结构是用哈希表实现的&#xff0c;只有精确匹配索引列的查询才有效&#xff0c;不支持…

Web版和客户端哪种SQL工具更好?ChatGPT有话要说

2023年年初公司发布了一款Web版SQL工具&#xff0c;短期内就赢得了众多用户的喜爱和下载。不过&#xff0c;也有SQL用户在评论区中提出自己的观点&#xff0c;认为Web版工具都不可靠&#xff0c;甚至看见Web版工具就劝返… … 工具Web化逐渐成为一种趋势&#xff0c;比如&…

如何使用Bypass-Url-Parser实现URL绕过并访问40X受保护页面

关于Bypass-Url-Parser Bypass-Url-Parser是一款功能强大的URL绕过工具&#xff0c;该工具可以使用多种方法实现URL绕过并访问目标站点的40X受保护页面。 工具下载 由于该工具基于Python 3 开发&#xff0c;因此我们首先需要在本地设备上安装并配置好Python 3环境。接下来&a…

PLC实验—西门子S7 1200读取超声波传感器数据

PLC实验—西门子S7 1200读取超声波传感器数据 US-016超声波测距模块 实验箱上是US-016超声波测距模块&#xff0c;其有关信息可以看实验室的博客 US-016超声波测距模块 1号Pin&#xff1a;接VCC电源&#xff08;直流5V&#xff09; 2号Pin&#xff1a;量程设置引脚&#xff…

GWT安装过程

1:安装前准备 &#xff08;可以问我要&#xff09; appengine-java-sdk-1.9.8 com.google.gdt.eclipse.suite.4.3.update.site_3.8.0 gwt-2.5.1 eclipse-jee-kepler-SR2-win32-x86_64.zip 2&#xff1a;安装环境上 打开eclipse Help –Install New Software… 选择Add –…

如何使用工时表管理项目和非项目的资源?

对新机会做出反应的能力是企业竞争优势的关键。项目不断涌现&#xff0c;企业需要了解具体的可用性以及是否有资源来接受新事物。更进一步来说&#xff0c;企业需要知道员工将时间花在哪里。 使用 8Manage工时表解决方案&#xff0c;你将始终拥有做出正确业务决策所需的全面知…

vue源码分析-响应式系统工作原理

上一章&#xff0c;我们讲到了Vue初始化做的一些操作&#xff0c;那么我们这一章来讲一个Vue核心概念响应式系统。 我们先来看一下官方对深入响应式系统的解释: 当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项&#xff0c;Vue 将遍历此对象所有的属性。 并使用 O…

LeetCode:构造最大二叉树;使用中序和后序数组构造二叉树;使用前序和中序数组遍历二叉树。

构造二叉树最好都是使用前序遍历&#xff1b;中左右的顺序。 654. 最大二叉树 中等 636 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点&#xff0c;其值为 nums 中的最大值。递归地在最大值 左边 的 子数组前缀上 构建…

人力资源管理系统

技术&#xff1a;Java、JSP等摘要&#xff1a;在当今的信息化社会&#xff0c;为了更有效率地工作&#xff0c;人们充分利用现在的电子信息技术&#xff0c;在办公室架设起办公服务平台&#xff0c;将人力资源相关信息统一起来管理&#xff0c;帮助管理者有效组织降低成本和加速…

【Linux】gcc/g++/gdb的使用

&#x1f525;&#x1f525; 欢迎来到小林的博客&#xff01;&#xff01;       &#x1f6f0;️博客主页&#xff1a;✈️小林爱敲代码       &#x1f6f0;️社区 : 进步学堂       &#x1f6f0;️欢迎关注&#xff1a;&#x1f44d;点赞&#x1f64c;收…

Mask R-CNN 算法学习总结

Mask R-CNN 相关知识点整体框架1.Resnet 深度残差学习1.1 目的1.2 深度学习深度增加带来的问题1.3 Resnet实现思想【添加恒等映射】2.线性插值2.1 目的2.2 线性插值原理2.3 为什么使用线性插值?3.FPN 特征金字塔3.1 FPN介绍3.2 为什么使用FPN?3.3 自下而上层【提取特征】3.4 …

反激与正激的区别

之前学习了正激开关电源&#xff0c;但是对于正激和反激一直不是很清楚&#xff0c;网上找了一篇&#xff0c;觉得感觉该可以&#xff0c;以此记录。正激和反激是两种不同的开关电源技术一、正激&#xff08;1&#xff09;概述正激式开关电源是指使用正激高频变压器隔离耦合能量…

深度学习笔记:神经网络权重确定初始值方法

神经网络权重不可为相同的值&#xff0c;比如都为0&#xff0c;因为如果这样网络正向传播输出和反向传播结果对于各权重都完全一样&#xff0c;导致设置多个权重和设一个权重毫无区别。我们需要使用随机数作为网络权重 实验程序 在以下实验中&#xff0c;我们使用5层神经网络…

设计模式-服务定位器模式

设计模式-服务定位器模式一、背景1.1 服务定位模式1.2 策略模式二、代码实战2.1 服务定位器2.2 配置ServiceLocatorFactoryBean2.3 定义一个支付的接口2.4 根据不同类型处理Bean2.5 controller层三、项目结构及测试结果3.1 测试结果3.2 项目结构及源码(欢迎star)四、参考资料一…

进程管理(进程概念、查看进程、关闭进程)

1.进程 程序运行在操作系统中&#xff0c;是被操作系统所管理的。 为管理运行的程序&#xff0c;每一个程序运行的时候&#xff0c;便被操作系统注册为系统中的一个&#xff1a;进程 并会为每一个进程都分配一个独有的&#xff1a;进程ID&#xff08;进程号&#xff09; 2. 查…