Word控件Spire.Doc 【Table】教程(6): 在 Word 中合并或拆分表格单元格

这篇具有很好参考价值的文章主要介绍了Word控件Spire.Doc 【Table】教程(6): 在 Word 中合并或拆分表格单元格。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

Spire.Doc for.NET 最新下载(qun:767755948)https://www.evget.com/product/3368/download

合并单元格是指将两个或多个单元格组合成一个较大的单元格,而拆分单元格是指将一个单元格分成两个或多个较小的单元格。在 Microsoft Word 中创建或编辑表格时,您可能经常需要合并或拆分表格单元格以更好地呈现数据。在本文中,您将学习如何使用Spire.Doc for .NET在 C# 和 VB.NET 中合并或拆分 Word 文档中的表格单元格

一、安装适用于 .NET 的 Spire.Doc

首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作为您的 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.Doc

二、使用 C# 和 VB.NET 在 Word 中合并表格单元格

在 Microsoft Word 中,您可以将两个或多个相邻的单元格水平或垂直合并为一个更大的单元格。在 Spire.Doc 中,您可以使用Table.ApplyHorizontalMerge()Table.ApplyVerticalMerge()方法实现相同的目的。以下是详细步骤:

  • 初始化Document类的一个实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 通过Document.Sections[int]属性通过索引获取文档中的特定部分。
  • 使用Section.AddTable()方法将表格添加到该部分。
  • 使用Table.ResetCells()方法指定表格的行数和列数。
  • 使用Table.ApplyHorizontalMerge()方法水平合并表格中的特定单元格。
  • 使用Table.ApplyVerticalMerge()方法垂直合并表格中的特定单元格。
  • 向表中添加一些数据。
  • 将样式应用于表格。
  • 使用Document.SaveToFile()方法保存结果文档。

【C#】

using Spire.Doc;
using Spire.Doc.Documents;

namespace MergeTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Input.docx");

//Get the first section
Section section = document.Sections[0];

//Add a 4 x 4 table to the section
Table table = section.AddTable();
table.ResetCells(4, 4);

//Horizontally merge cells 1, 2, 3, and 4 in the first row
table.ApplyHorizontalMerge(0, 0, 3);
//Vertically merge cells 3 and 4 in the first column
table.ApplyVerticalMerge(0, 2, 3);

//Add some data to the table
for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Rows[row].Cells.Count; col++)
{
TableCell cell = table[row, col];
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph paragraph = cell.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
paragraph.Text = "Text";
}
}

//Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1);

//Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace MergeTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Input.docx")

'Get the first section
Dim section As Section = document.Sections(0)

'Add a 4 x 4 table to the section
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)

'Horizontally merge cells 1, 2, 3, and 4 in the first row
table.ApplyHorizontalMerge(0, 0, 3)
'Vertically merge cells 3 and 4 in the first column
table.ApplyVerticalMerge(0, 2, 3)

'Add some data to the table
For row As Integer = 0 To table.Rows.Count - 1
For col As Integer = 0 To table.Rows(row).Cells.Count - 1
Dim cell As TableCell = table(row, col)
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
Dim paragraph As Paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
paragraph.Text = "Text"
Next
Next

'Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)

'Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

Word控件Spire.Doc 【Table】教程(6): 在 Word 中合并或拆分表格单元格,Spire.Doc for.net 教程(完),word,microsoft,spire.doc,文档管理,java

三、使用 C# 和 VB.NET 在 Word 中拆分表格单元格

Spire.Doc for .NET 提供了TableCell.SplitCell()方法,使您能够将 Word 表格中的一个单元格拆分为两个或多个单元格。以下是详细步骤:

  • 初始化Document类的一个实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 通过Document.Sections[int]属性通过索引获取文档中的特定部分。
  • 通过Section.Tables[int]属性根据其索引获取节中的特定表。
  • 通过Table.Rows[int].Cells[int]属性获取要拆分的表格单元格。
  • 使用TableCell.SplitCell()方法将单元格拆分为特定数量的列和行。
  • 使用Document.SaveToFile()方法保存结果文档。

【C#】

using Spire.Doc;

namespace SplitTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word Document
document.LoadFromFile("MergeCells.docx");

//Get the first section
Section section = document.Sections[0];

//Get the first table in the section
Table table = section.Tables[0] as Table;

//Get the 4th cell in the 4th row
TableCell cell1 = table.Rows[3].Cells[3];
//Split the cell into 2 columns and 2 rows
cell1.SplitCell(2, 2);

//save the result document
document.SaveToFile("SplitCells.docx", FileFormat.Docx2013);
}
}
}

【VB.NET】

Imports Spire.Doc

Namespace SplitTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word Document
document.LoadFromFile("MergeCells.docx")

'Get the first section
Dim section As Section = document.Sections(0)

'Get the first table in the section
Dim table As Table = TryCast(section.Tables(0), Table)

'Get the 4th cell in the 4th row
Dim cell1 As TableCell = table.Rows(3).Cells(3)
'Split the cell into 2 columns and 2 rows
cell1.SplitCell(2, 2)

'save the result document
document.SaveToFile("SplitCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

Word控件Spire.Doc 【Table】教程(6): 在 Word 中合并或拆分表格单元格,Spire.Doc for.net 教程(完),word,microsoft,spire.doc,文档管理,java

以上便是如何在 Word 中合并或拆分表格单元格,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。文章来源地址https://www.toymoban.com/news/detail-614833.html

到了这里,关于Word控件Spire.Doc 【Table】教程(6): 在 Word 中合并或拆分表格单元格的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Word控件Spire.Doc 【超链接】教程(3):在C#中查找word文档中的超链接

    Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处

    2024年02月04日
    浏览(35)
  • Spire.doc实现对word的操作(包括文字,表格,图片)

    Spire.doc比较小众,因此需要在pom.xml中导入spire.doc的仓库,导入之后直接导包 导入仓库后进行导包 2.1.1模板 首先需要准备一个word模板,可以在里面进行文字替换,因为文字特别多,所以此处采用了对具有特殊符号的文字进行替换。此处采用 ${xxx} 模板如图所示: 2.1.2替换核心

    2023年04月12日
    浏览(41)
  • table表格 ---合并单元格

            第一种方法                 利用table的 :header-cell-style属性           第二种方法                 利用官网提供的el-table-column互相嵌套           利用table的:span-method属性

    2024年02月07日
    浏览(27)
  • vxe-table表格合并单元格和编辑

    //这是在vue上面引用vxe-table插件实现的,主要方法都设置在table中,mergeCells,tableData都是在vue页面的data初使化数据, :footer-method=“footerMethod”:尾部数据,:merge-footer-items=“mergeCells”:尾部合并单元格。vxe-table网址:https://vxetable.cn/#/table/advanced/footerSpan

    2023年04月09日
    浏览(34)
  • poi-tl导出word复杂表格(单元格合并,生成复杂表格)

    官方文档地址:http://deepoove.com/poi-tl/ 源码地址:https://github.com/Sayi/poi-tl poi-tl(poi template language)是Word模板引擎,使用Word模板和数据创建很棒的Word文档。 最近在做项目时候有一个关于导出Word的文件的需求,需要导出的word文件较大,并且格式比较复杂,使用poi-tl可以很好的

    2024年02月16日
    浏览(34)
  • vue中Element-ui 表格 (Table)合并行、列单元格

    先在el-table里加个属性:span-method绑定方法objectSpanMethod(),这个用来把你想合并的列根据你写的逻辑来合并,再写个getSpanArr(),这个写合并的逻辑。 加一个属性两个方法,然后在获取表格数据的时候调用一下getSpanArr(),示例代码写了四个逻辑,根据id相同合并,根据id和其他字段

    2024年02月10日
    浏览(40)
  • vue前端docx库生成word表格 并合并单元格的例子

            Vue.js 是一个流行的前端JavaScript框架,用于构建用户界面和单页应用程序。在Vue中生成Word表格并合并单元格,通常需要使用额外的库,如`docx`,它是一个用于创建和修改Word文档(`.docx`)的JavaScript库。         以下是一个使用Vue.js和`docx`库来生成Word文档并合并

    2024年02月22日
    浏览(37)
  • 基于element-ui的table实现树级表格操作及单元格合并

    如题,公司业务需求,数据结构比较复杂,需要在一张表内实现多级树状数据展示及同属性的单元格合并,并在表格内实现增删改操作。 网上翻阅了很多实例,没有能解决所有需求的案例,于是自己实现了一套。 时间匆忙,逻辑有优化的地方还请无偿指出! 最终效果如下

    2024年02月14日
    浏览(34)
  • 前端vue+elementui导出复杂(单元格合并,多级表头)表格el-table转为excel导出

    需求 :前端对el-table表格导出 插件 : npm install xlsx -S npm install file-saver --save 原理 :直接导出el-table的表格里面的数据,这样就会存在缺点,只会导出当前页面的数据,如果需要导出全部数据,可以自己重新渲染一个全部数据不可见的el-table表格,来导出就可以了 扩展 :经过

    2024年02月04日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包