在DOC/DOCX文件中执行一个或多个表单元格的水平合并或垂直合并。 fileformat.words提供了使用Word文件中表的方法。

如何在Word文档中合并表单元格

概述

[fileformat.words] 2的较新版本提供了与[Word] 3文档中表合作的进一步方法。上一个[版本] 4包含用于创建,编辑和读取表属性的方法,而最新版本允许用户可以在DOCS/DOCX文件中编程合并表单元格。此外,您可以使用此开源.NET [library] 5进行水平合并或垂直合并。此外,这是一个易于使用的库,其方法并不复杂,并且不需要任何第三方依赖性。在此博客文章中,我们将学习如何在Word文档中合并表单元格。因此,让我们开始安装过程并开始编写源代码。 我们将介绍本文的以下标题:

  • [表Generator API安装] 6
  • [如何合并Word文档中的表单元格] 7

表Generator API安装

请访问此[链接] 8以获取有关安装的详细信息。仅需重新盖帽即可,此免费.NET API的安装过程相对容易。好吧,您可以下载[Nuget软件包] 9或在Nuget软件包管理器中运行以下命令:

Install-Package FileFormat.Words

如何在Word文档中合并表单元格

我们将编写一个代码片段,以实现表单元格的水平合并和垂直合并。为此,我们将使用暴露的类和方法。请访问本指南,以了解如何使用fileformat.words在Word文档中创建表。 因此,我们将在代码段中使用其他类和方法:

  • 创建[verticalmerge] 10类的对象。
  • [Mergerestart] 11属性用于指定该元素应在表中启动一个新的垂直合并区域。
  • 调用[Append] 12方法,将垂直器对象与TblCellProps对象连接。
  • 实例化[Horizo​​nTalmerge] 13类的实例。
  • [合并] 14属性用于指定该元素应在表中启动一个新的水平合并区域。
  • 调用[Append] 15方法,将Horizo​​nTalmerge对象与TblCellProps对象相连。
  • [MergeContinue] 16属性用于指定该元素应结束表中的水平合并区域。
  • [MergeContinue] 17属性用于指定该元素应结束表中的垂直合并区域。
using FileFormat.Words;
using FileFormat.Words.Table;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an instance of the Document class.
            using (Document doc = new Document())
            {
                // Instantiate the constructor of the Body class with the Document class object.
                Body body = new Body(doc);
                // Create an object of the Table class.
                Table table = new Table();
                // Initialize the constructor of the TopBorder class to set the border of the top side of the table.
                TopBorder topBorder = new TopBorder();
                // Invoke the basicBlackSquares_border method to set the border style and border line width.
                topBorder.basicBlackSquares_border(20);
                // To set the border of the bottom side of the table.
                BottomBorder bottomBorder = new BottomBorder();
                bottomBorder.basicBlackSquares_border(20);
                // To set the border of the right side of the table.
                RightBorder rightBorder = new RightBorder();
                rightBorder.basicBlackSquares_border(20);
                // To set the border of the left side of the table.
                LeftBorder leftBorder = new LeftBorder();
                leftBorder.basicBlackSquares_border(20);
                // To set the inside vertical border of the table.
                InsideVerticalBorder insideVerticalBorder = new InsideVerticalBorder();
                insideVerticalBorder.basicBlackSquares_border(20);
                // To set the inside vehorizontalrtical border of the table.
                InsideHorizontalBorder insideHorizontalBorder = new InsideHorizontalBorder();
                insideHorizontalBorder.basicBlackSquares_border(20);
                // Create an instance of the TableBorders class. 
                TableBorders tableBorders = new TableBorders();
                // Append the object of the TopBorder class to the object of the TableBorders class.
                tableBorders.AppendTopBorder(topBorder);
                // Append the object of the BottomBorder class.
                tableBorders.AppendBottomBorder(bottomBorder);
                // Append the object of the RightBorder class.
                tableBorders.AppendRightBorder(rightBorder);
                // Append the object of the LeftBorder class.
                tableBorders.AppendLeftBorder(leftBorder);
                // Append the object of the InsideVerticalBorder class.
                tableBorders.AppendInsideVerticalBorder(insideVerticalBorder);
                // Append the object of the InsideHorizontalBorder class.
                tableBorders.AppendInsideHorizontalBorder(insideHorizontalBorder);

                // Initialize an instance of the TableProperties class.
                TableProperties tblProp = new TableProperties();
                // Invoke the Append method to attach the object of the TableBorders class.
                tblProp.Append(tableBorders);
                // Create an instance of the TableJustification class 
                TableJustification tableJustification = new TableJustification();
                // Call the AlignLeft method to position the table on left side of the document.
                tableJustification.AlignLeft();
                // Invoke the Append method to attach the tableJustification object to the tblProp object.
                tblProp.Append(tableJustification);

                // The AppendChild method will attach the table properties to the table.
                table.AppendChild(tblProp);

                // Create an object of the TableRow class to create a table row.
                TableRow tableRow = new TableRow();
                TableRow tableRow2 = new TableRow();

                // Initialize an instance of the TableCell class.
                TableCell tableCell = new TableCell();
                Paragraph para = new Paragraph();
                Run run = new Run();

                // Set the header of the first column by invoking the TableHeaders method.
                table.TableHeaders("Name");
                run.Text = "Mustafa";
                para.AppendChild(run);
                // Call the Append method to add text inside the table cell.
                tableCell.Append(para);

                // Create an object of the TableCellProperties table properties 
                TableCellProperties tblCellProps = new TableCellProperties();

                // Set the width of table cell by initializing the object of the TableCellWidth class and append to tblCellProps object.
                tblCellProps.Append(new TableCellWidth("2400"));
                // Append method will attach the tblCellProps object with the object of the TableCell class.
                tableCell.Append(tblCellProps);

                TableCell tableCell2 = new TableCell();
                Paragraph para2 = new Paragraph();
                Run run2 = new Run();

                // set the header of the second column
                table.TableHeaders("Nationality");
                run2.Text = "Pakistani";
                para2.AppendChild(run2);
                tableCell2.Append(para2);

                TableCellProperties tblCellProps2 = new TableCellProperties();
                // Create an object of the VerticalMerge class. 
                VerticalMerge verticalMerge = new VerticalMerge();
                // MergeRestart property is used to specify that the element shall start a new vertically merged region in the table.
                verticalMerge.MergeRestart = true;
                // Invoke the Append method to attach the verticalMerge object with the tblCellProps object.
                tblCellProps.Append(verticalMerge);

                // Instantiate an instance of the HorizontalMerge class. 
                HorizontalMerge horizontalMerge = new HorizontalMerge();
                // MergeRestart property is used to specify that the element shall start a new horizontally merged region in the table.
                horizontalMerge.MergeRestart = true;
                // Call the Append method to attach the horizontalMerge object with the tblCellProps object.
                tblCellProps2.Append(horizontalMerge);

                tblCellProps2.Append(new TableCellWidth("1400"));
                tableCell2.Append(tblCellProps2);

                TableCell tableCell3 = new TableCell();
                Paragraph para3 = new Paragraph();
                Run run3 = new Run();
                table.TableHeaders("Age");
                run3.Text = "30";
                para3.AppendChild(run3);
                tableCell3.Append(para3);

                HorizontalMerge horizontalMerge1 = new HorizontalMerge();
                // MergeContinue property is used to specify that the element shall end a horizontally merged region in the table.
                horizontalMerge1.MergeContinue = true;
                TableCellProperties tblCellProps3 = new TableCellProperties();
                tblCellProps3.Append(new TableCellWidth("1400"));
                tblCellProps3.Append(horizontalMerge1);

                tableCell3.Append(tblCellProps3);
                // Call the Append method to add cells into table row.
                tableRow.Append(tableCell);
                tableRow.Append(tableCell2);
                tableRow.Append(tableCell3);

                // create table cell
                TableCell _tableCell = new TableCell();
                Paragraph _para = new Paragraph();
                Run _run = new Run();

                _run.Text = "sultan";
                _para.AppendChild(_run);
                _tableCell.Append(_para);

                TableCellProperties tblCellProps1_ = new TableCellProperties();
                VerticalMerge verticalMerge2 = new VerticalMerge();
                // MergeContinue property is used to specify that the element shall end a vertically merged region in the table.
                verticalMerge2.MergeContinue = true;
                tblCellProps1_.Append(verticalMerge2);
                tblCellProps1_.Append(new TableCellWidth("2400"));
                _tableCell.Append(tblCellProps1_);


                TableCell _tableCell2 = new TableCell();
                Paragraph _para2 = new Paragraph();
                Run _run2 = new Run();

                _run2.Text = "British";
                _para2.AppendChild(_run2);
                _tableCell2.Append(_para2);

                TableCellProperties tblCellProps2_ = new TableCellProperties();
                tblCellProps2_.Append(new TableCellWidth("1400"));
                _tableCell2.Append(tblCellProps2_);

                TableCell _tableCell3 = new TableCell();
                Paragraph _para3 = new Paragraph();
                Run _run3 = new Run();

                _run3.Text = "2";
                _para3.AppendChild(_run3);
                _tableCell3.Append(_para3);

                TableCellProperties tblCellProps3_ = new TableCellProperties();
                tblCellProps3_.Append(new TableCellWidth("1400"));
                _tableCell3.Append(tblCellProps3_);

                tableRow2.Append(_tableCell);
                tableRow2.Append(_tableCell2);
                tableRow2.Append(_tableCell3);
                // Call the Append method to add the rows into table.
                table.Append(tableRow);
                table.Append(tableRow2);
                // The AppendChild method will add the table to the body of the document.
                body.AppendChild(table);
                // The Save method will save the Word document onto the disk.
                doc.Save("/Users/Mustafa/Desktop/Docs.docx");
            }

        }

    }
}

将上述代码段复制到主文件中并运行。您将看到与下图中显示的内容生成的单词文件:

合并表单元

结论

我们正在此处结束这篇博客文章,希望您学会了如何以编程方式在Word **中合并表单元格。此外,我们还编写了源代码以实现表单元格的水平合并和垂直合并。因此,您可以选择此开源.NET **表Generator API **自动化Word文件自动化。最后,不要忘记访问[文档] 19以了解更多的课程和方法。 最后,[fileformat.com] 20一直在撰写有关有趣主题的教程博客文章。因此,请保持联系以进行定期更新。此外,您可以在我们的社交媒体平台上关注我们,包括[Facebook] 21,[LinkedIn] 22和[Twitter] 23

贡献

由于[.NET的FileFormat.Words] 5是一个开源项目,可在[GitHub] 24上找到。因此,社区的贡献非常感谢。

问一个问题

您可以在我们的[论坛] 25上让我们知道您的问题或查询。

常见问题

我如何在Word文档中合并单元格? 您可以使用[Verticalmerge] 10和[Horizo​​ntalmerge] 13类合并表单元格。 我如何在DOC中的表中合并单元格? 请按照此[链接] 7学习步骤和代码片段以实现此功能。

另请参见

  • [如何使用fileformat.words在C#中创建Word文档] 8
  • [如何使用fileformat.words在C#中编辑Word文档] 26
  • [如何使用fileformat.words在Word文件中制作表] 27