首页 > 编程语言 >借助 Aspose.Words,在 Word 文档中创建表格

借助 Aspose.Words,在 Word 文档中创建表格

时间:2024-08-13 14:25:16浏览次数:8  
标签:创建表格 Word doc builder Cell 文档 Words table

Word 文档中的表格是一种强大的工具,可用于以清晰、结构化的格式组织和呈现数据。表格由行和列组成,行和列相交形成可包含文本、数字、图像或其他元素的单元格。在本文中,我们将学习如何使用 C# 以编程方式在 Word 文档中创建表格。本文通过代码示例展示了在DOCX文件中创建表格的各种方法。

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

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

Aspose.words for.net下载   Aspose.words for for java下载

在 Word 文档中创建表格的 C# 库

为了处理 Word 文档中的表格,我们将使用Aspose.Words for .NET库。这是一个强大的库,可让您直接在 .NET 应用程序中以编程方式动态创建和操作 Word 文档。

请使用以下命令下载 DLL或从NuGet安装它:

PM> Install-Package Aspose.Words
使用 C# 在 Word 文档中创建表格

有两种方法可以使用 Aspose.Words for .NET 在 Word 文档中创建表格:

  • 使用 DocumentBuilder 类
  • 使用 DOM(文档对象模型)

您可以选择最适合您需求的方法。让我们详细探讨每种方法。

使用 DocumentBuilder 创建表

DocumentBuilder可以高效、轻松地从头开始创建动态文档或修改现有文档。借助其全面的功能,我们可以无缝插入各种内容元素,包括文本、复选框、OLE 对象、段落、列表、表格、图像等等。

请按照以下步骤使用 DocumentBuilder 类在 Word 文档中创建表格。

  1. 创建Document类的对象。
  2. 创建DocumentBuilder类的对象。
  3. 使用StartTable()方法创建一个新表。
  4. 使用InsertCell()方法插入一个单元格。
  5. 使用Write()方法将文本插入单元格。
  6. 根据需要重复插入单元格并将文本插入单元格。
  7. 使用EndRow()方法结束一行。
  8. 使用EndTable()方法结束表。
  9. 最后,使用Save()方法保存Word文档。

以下代码示例展示如何使用 C# 在 Word 文档中创建表格。

// This code example demonstrates how to create a table in a Word document using C#
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");

// Build the second cell.
builder.InsertCell();
builder.Write("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("Row 2, Cell 1 Content");

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

// Signal that we have finished building the table.
builder.EndTable();

doc.Save("CreateSimpleTable.docx");

使用 DocumentBuilder 在 Word 中创建表格

使用文档对象模型 (DOM) 创建表格

文档对象模型 (DOM)是Word 文档的内存表示形式。它允许通过编程方式读取、操作和修改 Word 文档的内容和格式。

请按照以下步骤使用 DOM 在 Word 文档中创建表格。

  1. 创建Document类的对象。
  2. 使用Table()类创建一个新表。
  3. 使用AppendChild()方法将表格添加到文档主体。
  4. 创建Row类的对象并使用Table.AppendChild(Row)方法将其插入表中。
  5. 创建Cell类的对象,设置格式选项,并向单元格添加文本。
  6. 使用Row.AppendChild(Cell)方法将单元格插入到行中。
  7. 之后,根据需要重复该过程以获得尽可能多的行。
  8. 最后,使用Save()方法保存Word文档。

以下代码示例展示如何使用 C# 在 Word 文档中创建表格。

// This code example demonstrates how to create a table in a Word document using DOM in C#
Document doc = new Document();

// We start by creating the table object. Note that we must pass the document object
// to the constructor of each node. This is because every node we create must belong
// to some document.
Table table = new Table(doc);
doc.FirstSection.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.

// 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 algorithm.
Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);

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

Cell cell = new Cell(doc);
cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
cell.CellFormat.Width = 80;
cell.AppendChild(new Paragraph(doc));
cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 1 Text"));

// Append a cell
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(cell.Clone(false));
row.LastCell.AppendChild(new Paragraph(doc));
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text"));

// Save the document
doc.Save("InsertTableDirectly.docx");

使用文档对象模型 (DOM) 创建表格

使用 C# 在 Word 文档中创建嵌套表格

我们还可以在表格的单元格内创建新表格。以下是在 Word 文档中创建嵌套表格的步骤。

  1. 创建Document类的对象。
  2. 创建DocumentBuilder类的对象。
  3. 使用StartTable()方法创建一个表并获取对象中对该表的引用。
  4. 使用InsertCell()方法插入一个单元格并获取对象中对该单元格的引用。
  5. 使用DocumentBuilder.Write()方法将文本插入单元格。
  6. 根据需要重复插入单元格并将文本插入单元格。
  7. 插入所有行后结束表格。
  8. 使用MoveTo(cell.FirstParagraph)方法将控制权移至所需单元格内。
  9. 通过插入单元格来创建另一个表格,完成后结束表格。
  10. 最后,使用Save()方法保存Word文档。

以下代码示例展示如何使用 C# 在 Word 文档中创建嵌套表格。

// This code example demonstrates how to create a nested table in a Word document using C#
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Cell cell = builder.InsertCell();
builder.Writeln("Outer Table Cell 1");

builder.InsertCell();
builder.Writeln("Outer Table Cell 2");

// This call is important 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.FirstParagraph);

// Build the inner table.
builder.InsertCell();
builder.Writeln("Inner Table Cell 1");
builder.InsertCell();
builder.Writeln("Inner Table Cell 2");
builder.EndTable();

// Save the document
doc.Save("NestedTable.docx");

使用 C# 在 Word 文档中创建嵌套表格

使用 C# 克隆 Word 文档中的现有表格

我们可以按照以下步骤克隆Word文档中现有的表格:

  1. 使用Document类加载带有表的现有文档。
  2. 使用GetChild(NodeType.TABLE, int, boolean)方法获取对象中的表。
  3. 使用Table.Clone(True)方法克隆表。
  4. 使用Table.ParentNode.InsertAfter()方法插入克隆表。
  5. 使用Table.ParentNode.InsertAfter(new Paragraph(Document), Table)方法在表格之间插入一个空段落。
  6. 最后,使用Save()方法保存Word文档。

以下代码示例展示如何使用 C# 克隆 Word 文档中的表格。

// This code example demonstrates how to clone an existing table in a Word document using C#
Document doc = new Document("Tables.docx");

Table table = (Table) doc.GetChild(NodeType.Table, 0, true);

// Clone the table and insert it into the document after the original.
Table tableClone = (Table) table.Clone(true);
table.ParentNode.InsertAfter(tableClone, table);

// Insert an empty paragraph between the two tables,
// or else they will be combined into one upon saving this has to do with document validation.
table.ParentNode.InsertAfter(new Paragraph(doc), table);

doc.Save("CloneCompleteTable.docx");

使用 C# 克隆 Word 文档中的现有表格

通过 HTML 在 Word 文档中创建表格

我们还可以按照以下步骤使用 HTML 字符串在 Word 文档中创建表格:

  1. 创建Document类的对象。
  2. 创建DocumentBuilder类的对象。
  3. 使用DocumentBuilder.InsertHtml(String)方法将表的 HTML 字符串插入文档。
  4. 最后,使用Document.Save()方法保存文档。

以下代码示例显示如何使用 C# 在 Word 文档中插入 HTML 表格。

// This code example demonstrates how to insert an HTML table in a Word document using C#
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

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

doc.Save("InsertTableFromHtml.docx");

通过 HTML 在 Word 文档中创建表格

在本文中,我们学习了如何使用 C# 在 Word 文档中创建表格。我们探索了使用 C# 以编程方式创建表格的各种方法。我们还了解了如何创建嵌套表格或动态克隆 Word 文档中的现有表格。

标签:创建表格,Word,doc,builder,Cell,文档,Words,table
From: https://blog.csdn.net/m0_67129275/article/details/141127523

相关文章

  • 【办公软件学习】解决在打开word时,出现 “word 在试图打开文件时遇到错误” 的问题
    1.问题描述:最近在网上查找期刊论文的模板时,发现从期刊官网下载下来的论文格式模板,在本地用word打开时,出现错误,情况如下2.解决办法关闭提示窗口,打开左上角的【文件】按钮点击【选项】按钮点击【信任中心】>>>>【信任中心设置】选择【受保护视图】选项卡,将右侧窗口中......
  • 【办公软件学习】如何将Word格式转换为Markdown格式
    一键!将Word转换为Markdown参考链接1:https://zhuanlan.zhihu.com/p/30891168参考链接2:https://blog.csdn.net/qq15035899256/article/details/125547483参考链接3:https://word2md.com/方法一:Writage+Pandoc—双剑合璧!下载并安装Writage,下载地址:http://www.writage.c......
  • 打开Office(word、excel、ppt)显示操作系统当前的配置不能运行此应用程序最全解决方案
    我以前用过分区助手把office从c盘挪到d盘了,从那以后office就用不了了,然后我就删了(貌似没删干净)。最近由于有使用word的需求,所以我从学校官网找到正版软件的安装包,按照步骤重新卸载电脑中office残留并安装好之后,打开word会提示“操作系统当前的配置不能运行此应用程序”,ppt......
  • Word2Vec模型介绍
    Word2Vec是一种用于生成词向量的模型,由TomasMikolov等人在2013年提出。它通过从大量语料库中学习,捕捉词汇之间的语义关系。Word2Vec主要包括两种模型架构:1.CBOW(ContinuousBagofWords)模型CBOW模型通过上下文词来预测中心词。它的工作原理如下:输入:上下文词(例如,选取......
  • Word2Vec模型之CBOW
    CBOW(ContinuousBagofWords)是一种常用于自然语言处理的词嵌入模型,属于Word2Vec的一部分。CBOW模型的目标是通过上下文词来预测中心词。它通过在大规模语料库中学习词汇之间的共现关系,生成词向量表示。CBOW模型的工作原理上下文窗口:CBOW模型的核心思想是利用上下文窗口中的词......
  • 基于模拟退火算法求解旅行商(TSP)问题(附word文档)
    基于模拟退火算法求解旅行商(TSP)问题(附word文档)......
  • WPS2019视频教程:表格制作excel教程、Word文字排版处理、PPT制作
    WPS2019视频教程《表格制作excel教程、Word文字排版处理、PPT制作动画设置》视频教学课程内容目录:下载地址:https://pan.quark.cn/s/d44896d73234WPS表格制作excel教程视频表格制作图表数据透视表公式与函数(P1.1-1:表格界面介绍).flv表格制作图表数据透视表公式与函......
  • go项目实战之word、pdf、txt操作
    最近在项目开发中,频繁的遇到需要对Docx、PDF、TXT等类型的文本进行操作,而目前这方面有unidoc/unioffice,但这个是非开源的,所以使用起来有诸多不方便。而且也搜了很多资料,但是都太笼统了,不方便使用,所以特写此文章希望能帮助大家解决问题!以下代码都可直接复制粘贴使用DO......
  • 宝塔面板安装后wordpress优化教程
    宝塔面板安装wordpress之后,一直有一个头痛的问题按F5会导致cpu和内存100%,这个问题也困扰了我一个月,后面也是各种解决方案都不太理想!现在我出来一个教程,按我的思路我的服务器是2H2G的阿里云服务器!里面只有一个wordpress站点,也就是现在你所看到的站点运行环境:nginx1.......
  • KINDEDITOR富文本实现导入WORD功能
    昨天晚上有网友给我发私信请教这个问题,如何解决word内容复制粘贴的问题,这位网友也是刚开始学习接触这块,很多基础知识并不了解,比如如何上传图片,如何保存图片,这些技术他都不了解。先花了大概半个小时左右的时间给他讲解了一下基础知识,然后又花了一个小时的时间帮他搭建了一个开发......