Docs/docxファイルで1つ以上のテーブルセルの水平マージまたは垂直マージを実行します。 fileformat.wordsは、単語ファイルのテーブルを使用する方法を提供します。

単語文書でテーブルセルをマージする方法

概要

[fileformat.words] 2の新しいバージョンは、[Word] 3ドキュメントでテーブルを操作するためのさらなる方法を提供します。以前の[バージョン] 4には、テーブルプロパティを作成、編集、および読み取りする方法が含まれていますが、最新バージョンでは、ユーザーがドキュメント/docxファイルにテーブルセルをプログラム的にマージできます。さらに、このオープンソース.NET [ライブラリ] 5を使用して、テーブルセルの水平マージまたは垂直マージ を実行できます。さらに、それは使いやすいライブラリであり、その方法は複雑ではなく、サードパーティの依存関係を必要としません。このブログ投稿では、単語文書にテーブルセルをマージする方法を学びます。それでは、インストールプロセスを開始して、ソースコードの書き込みを開始しましょう。 この記事では、次の見出しについて説明します。

  • [テーブルジェネレーターAPIインストール] 6
  • [単語文書でテーブルセルをマージする方法] 7

テーブルジェネレーターAPIインストール

インストールの詳細については、この[リンク] 8をご覧ください。再キャップのために、この無料の.NET APIのインストールプロセスは比較的簡単です。さて、[nugetパッケージ] 9をダウンロードするか、Nugetパッケージマネージャーで次のコマンドを実行できます。

Install-Package FileFormat.Words

単語文書でテーブルセルをマージする方法

テーブルセルの水平マージと垂直マージを実現するために、コードスニペットを作成します。その目的のために、露出したクラスと方法を使用します。このガイドにアクセスして、fileformat.wordsを使用して単語ドキュメントでテーブルを作成する方法を学びます。 そのため、コードスニペットでさらにクラスとメソッドを使用します。

  • [verticalmerge] 10クラスのオブジェクトを作成します。
  • [Mergerestart] 11プロパティは、要素がテーブル内の新しい垂直統合領域を起動することを指定するために使用されます。
  • [append] 12メソッドを呼び出して、verticalmergeオブジェクトをtblcellpropsオブジェクトに接続します。
  • [horizo​​ntalmerge] 13クラスのインスタンスをインスタンスします。
  • [Mergerestart] 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");
            }

        }

    }
}

上記のコードセグメントをメインファイルにコピーして貼り付けて実行します。以下の画像に示されているコンテンツで生成された単語ファイルが表示されます。

テーブルセルをマージします

結論

このブログ投稿は、プログラムでテーブルセルをドキュメントにマージする方法を マージする方法を学んだことを期待して、このブログ投稿を終了しています。さらに、テーブルセルの水平マージと垂直マージを実装するためのソースコードも書きました。したがって、このオープンソース.NET テーブルジェネレーターAPI を選択して、ワードファイルの自動化を自動化できます。最後に、[ドキュメント] 19にアクセスして、さらなるクラスと方法について学ぶことを忘れないでください。 最後に、[fileformat.com] 20は、興味深いトピックに関するチュートリアルブログ投稿を一貫して執筆しています。したがって、定期的な更新については連絡を取り合いましょう。さらに、[Facebook] 21、[LinkedIn] 22、[Twitter] 23など、ソーシャルメディアプラットフォームでフォローできます。

貢献

[.netのfileformat.words] 5はオープンソースプロジェクトであり、[github] 24で入手できます。したがって、コミュニティからの貢献は大歓迎です。

質問する

[フォーラム] 25での質問や質問についてお知らせください。

FAQS

単語文書でセルをマージするにはどうすればよいですか? [verticalmerge] 10および[horizo​​ntalmerge] 13クラスを使用して、テーブルセルをマージできます。 docのテーブルにセルをマージするにはどうすればよいですか この[リンク] 7に従って、この機能を実現するための手順とコードスニペットを学習してください。

{.wp-block-heading}も参照してください

  • [fileformat.wordsを使用してc#で単語ドキュメントを作成する方法] 8
  • [fileformat.wordsを使用してc#で単語ドキュメントを編集する方法] 26
  • [fileformat.wordsを使用して単語ファイルでテーブルを作成する方法] 27