首页 > 其他分享 >如何合并与拆分 Word 表格中的单元格

如何合并与拆分 Word 表格中的单元格

时间:2023-02-03 10:35:20浏览次数:54  
标签:document Word 表格 单元格 文档 拆分 table Document

在编辑Word文档时插入表格是非常实用的功能。有时,合并或者拆分某些单元格能帮助我们更好的显示或者编辑表格数据。在这里我将分享如何通过编程的方法完成此项操作。具体操作步骤后面附有示例代码,供参考使用。

程序

在这篇教程中,我所使用的类库是Free Spire.Doc for .NET,可以通过以下两种方法安装:

方法一

    通过NuGet安装Free Spire.Doc for .NET,具体步骤为:依次选择工具>NuGet包管理器>程序包管理器控制台,然后执行以下命令:

    PM> Install-Package FreeSpire.Doc      

方法二

    在程序中手动引入Spire.doc.dll文件;将Free Spire.Doc for .NET 下载到本地,解压并安装。安装完成后,打开Visual Studio创建新项目,在右边的“解决方案资源管理器”中右键点击“引用”,再依次选择“添加引用”> “浏览”,找到安装路径下BIN文件夹中的dll文件,点击“确定”,将其添加引用至程序中。

 

合并 Word 表格中的元格

  • 初始化 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)
        {
            //初始化 Document类的实例
            Document document = new Document();
            //加载Word文档
            document.LoadFromFile("sample.docx");

            //获取特定节
            Section section = document.Sections[0];

            //添加一个 4 x 4 表格到该节
            Table table = section.AddTable();
            table.ResetCells(4, 4);

            //水平合并表中的特定单元格
            table.ApplyHorizontalMerge(0, 0, 3);
            //垂直合并表中的特定单元格
            table.ApplyVerticalMerge(0, 2, 3);

            //将数据添加到表格中
            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 = "示例";
                }
            }

            //将样式应用于表
            table.ApplyStyle(DefaultTableStyle.LightGridAccent1);

            //保存结果文档
            document.SaveToFile("result.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())
            '初始化 Document类的实例
            Dim document As Document = New Document()
            '加载Word文档
            document.LoadFromFile("sample.docx")

            '获取特定节
            Dim section As Section = document.Sections(0)

            '添加一个 4 x 4 表格到该节
            Dim table As Table = section.AddTable()
            table.ResetCells(4, 4)

            '水平合并表中的特定单元格
            table.ApplyHorizontalMerge(0, 0, 3)
            '垂直合并表中的特定单元格
            table.ApplyVerticalMerge(0, 2, 3)

            '将数据添加到表格中
            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 = "示例"
                Next
            Next

            '将样式应用于表
            table.ApplyStyle(DefaultTableStyle.LightGridAccent1)

            '保存结果文档
            document.SaveToFile("result.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

拆分 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)
        {
            //初始化Document类的实例
            Document document = new Document();
            //加载Word文档
            document.LoadFromFile("rusult.docx");

            //获取文档中的特定节
            Section section = document.Sections[0];

            //在该节获取特定表格
            Table table = section.Tables[0] as Table;

            //获取要拆分的表格单元格
            TableCell cell1 = table.Rows[3].Cells[3];
            //将单元格分为特定数量的列和行
            cell1.SplitCell(2, 2);

            //保存结果文档
            document.SaveToFile("result2.docx", FileFormat.Docx2013);
        }
    }
}

VB.NET:

Imports Spire.Doc

Namespace SplitTableCells
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '初始化Document类的实例
            Dim document As Document = New Document()
            '加载Word文档
            document.LoadFromFile("result.docx")

            '获取文档中的特定节
            Dim section As Section = document.Sections(0)

            '在该节获取特定表格
            Dim table As Table = TryCast(section.Tables(0), Table)

            '获取要拆分的表格单元格
            Dim cell1 As TableCell = table.Rows(3).Cells(3)
            '将单元格分为特定数量的列和行
            cell1.SplitCell(2, 2)

            '保存结果文档
            document.SaveToFile("result2.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

标签:document,Word,表格,单元格,文档,拆分,table,Document
From: https://www.cnblogs.com/Gia-/p/17088317.html

相关文章

  • .NET WORD转PDF 免费方案整理
    引用WPS(或MSOffice)COMASP.NET使用WPSWORD转PDFC#实现MS-Office文档转PdfWpsToPdf.git完全免费,再也不用担心转pdf文件乱来乱去的问题了Aspose.wordAspose.Words......
  • WordPress4.6任意命令执行漏洞
    前言WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也算是一个内容管理系统(CMS)环境搭建docker环境(......
  • 理解Word2Vec
    为什么输出矩阵的第i行就是单词ωi的输出向量这个问题等同于下面这个式子(这个是CBOW的):这里我画了个草图来说明原因:......
  • DataEase 启动异常如何解决:Access denied for user 'root'@'xx.xx.xx.xxx' (using pas
    异常信息:SQLState:28000ErrorCode:1045Message:Accessdeniedforuser'root'@'xx.xx.xx.xxx'(usingpasswordYES)异常原因分析:我的DataEase部署方式是整包部......
  • [LeetCode]Length of Last Word
    QuestionGivenastringsconsistsofupper/lower-casealphabetsandemptyspacecharacters​​​''​​,returnthelengthoflastwordinthestring.Ifthe......
  • 报错:Unexpected reserved word 'await'?
    async和await是成对出现的。会报上边的错是因为没有把async放在和await最近的函数上。举例:报错代码:1asyncconfirmAll(){2this.$refs['editeForm'].valida......
  • PHPMyWind支持Word一键粘贴
    ​ 百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复......
  • PHPMyWind支持Word粘贴
    ​ 图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.j......
  • 用python把word转成pdf
    需要使用python-docx模块pipinstallpython-docx然后函数主体importosimportdocx2pdfdefword_to_pdf(file_path):pdf_file=file_path.replace(".docx"......
  • plugin caching_sha2_password could not be loaded
    参考连接:https://blog.csdn.net/qq_54202620/article/details/122729704https://blog.csdn.net/qq_45214414/article/details/115284609 navicate可以连接,但是SQLyog不......