在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