DOCS/DOCX dosyalarında bir veya daha fazla tablo hücresinin yatay birleştirilmesi veya dikey birleştirilmesi yapın. FileFormat.Words, kelime dosyalarındaki tablolarla çalışma yöntemleri sağlar. {.wp-blok başlığı}

Kelime belgelerindeki tablo hücreleri nasıl birleştirilir

Genel Bakış

[FileFormat.words] 2 ‘in daha yeni sürümü, [Word] 3 belgelerindeki tablolarla çalışmak için daha fazla yöntem sunar. Önceki [sürüm] 4, tablo özellikleri oluşturma, düzenleme ve okuma yöntemleri içerirken, en son sürüm kullanıcıların DOCS/DOCX dosyalarındaki tablo hücrelerini programlı olarak birleştirmelerini sağlar. Ayrıca, bu açık kaynak .NET [kütüphane] 5 kullanarak tablo hücrelerinin yatay birleştirme veya dikey birleştirme **yapabilirsiniz. Dahası, yöntemleri karmaşık olmayan ve herhangi bir üçüncü taraf bağımlılığına ihtiyaç duymayan kullanımı kolay bir kütüphanedir. Bu blog yazısında, masa hücrelerini kelime belgelerde nasıl birleştireceğinizi öğreneceğiz. Öyleyse, yükleme işlemini başlatalım ve kaynak kodu yazmaya başlayalım. Bu makaledeki aşağıdaki başlıkları ele alacağız:

  • [Tablo Generator API kurulumu] 6
  • [Kelime belgelerindeki tablo hücreleri nasıl birleştirilir] 7

Tablo Generator API kurulumu

Kurulum hakkında ayrıntılı bilgi için lütfen bu [link] 8 ziyaret edin. Sadece yeniden kapmak için, bu ücretsiz .NET API’sının kurulum işlemi nispeten kolaydır. Peki, [Nuget Paket] 9 ‘u indirebilir veya Nuget Paket Yöneticisi’nde aşağıdaki komutu çalıştırabilirsiniz:

Install-Package FileFormat.Words

Kelime belgelerindeki tablo hücreleri nasıl birleştirilir

Tablo hücrelerinin yatay birleştirilmesi ve dikey birleştirilmesi elde etmek için bir kod snippet’i yazacağız. Bu amaçla maruz kalan sınıfları ve yöntemleri kullanacağız. FileFormat.words kullanarak bir Word belgesinde tablo oluşturmayı öğrenmek için lütfen bu kılavuzu ziyaret edin. Bu nedenle, kod snippet’imizde daha fazla sınıf ve yöntem kullanacağız:

  • [Dikeyerge] 10 sınıfının bir nesnesi oluşturun.
  • [Mergerestart] 11 Özellik, elemanın tabloda dikey olarak birleştirilmiş yeni bir bölge başlatacağını belirtmek için kullanılır.
  • Dikeyselge nesnesini TBLCellProps nesnesine eklemek için [ek] 12 yöntemini çağırın.
  • [Horizontalmerge] 13 sınıfının bir örneğini başlatın.
  • [MERGERESTART] 14 Özellik, elemanın tabloda yatay olarak birleştirilmiş yeni bir bölge başlatacağını belirtmek için kullanılır.
  • TBLCellProps nesnesiyle Horizontalmerge nesnesini eklemek için [Ek] 15 yöntemini çağırın.
  • [MergeContinue] 16 Özellik, elemanın tablodaki yatay olarak birleştirilmiş bir bölgeyi sona erdireceğini belirtmek için kullanılır.
  • [MergeContinue] 17 Özellik, elemanın tablodaki dikey olarak birleştirilmiş bir bölgeyi sona erdireceğini belirtmek için kullanılır.
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");
            }

        }

    }
}

Yukarıdaki kod segmentini kopyalayıp ana dosyanıza yapıştırın ve çalıştırın. Aşağıdaki resimde gösterilen içerikle oluşturulan dosyayı göreceksiniz:

Birleştirme Masa Hücreleri

Sonuç

Bu blog gönderisini burada Word ** belgelerde birleştirmeyi öğrendiğiniz umuduyla bitiriyoruz. Ayrıca, tablo hücrelerinin yatay birleştirme ve dikey birleştirme uygulamak için kaynak kodu yazdık. Bu nedenle, kelime dosyası otomasyonunu otomatikleştirmek için bu açık kaynaklı .net tablo jeneratör API seçebilirsiniz. Sonunda, daha fazla sınıf ve yöntem hakkında bilgi edinmek için [dokümantasyon] 19 ‘u ziyaret etmeyi unutmayın. Son olarak, [FileFormat.com] 20 ilginç konulara sürekli olarak öğretici blog yayınları yazıyor. Bu nedenle, düzenli güncellemeler için lütfen iletişim halinde olun. Dahası, bizi [Facebook] 21, [LinkedIn] 22 ve [Twitter] 23 dahil olmak üzere sosyal medya platformlarımızda takip edebilirsiniz.

katkıda bulun

Çünkü [.NET] [FileFormat.Words] 5 açık kaynaklı bir projedir ve [GitHub] 24 ’te mevcuttur. Dolayısıyla, topluluktan katkı çok takdir ediliyor.

Bir soru sorun

[Forum] 25 ‘de sorularınızı veya sorgularınızı bize bildirebilirsiniz.

SSS {.wp-blok başlık}

Bir kelime belgesinde hücreleri nasıl birleştirebilirim? Tablo hücrelerini [dikeyerge] 10 ve [horizontalmerge] 13 sınıflarını kullanarak birleştirebilirsiniz. DOC’taki bir masada hücreleri nasıl birleştirebilirim? Bu işlevselliğe ulaşmak için adımları ve kod snippet’ini öğrenmek için lütfen bunu [link] 7 izleyin.

Ayrıca bkz.

  • [FileFormat.words kullanarak C# ‘da Word belgesi nasıl oluşturulur] 8
  • [FileFormat.words kullanarak C# ‘da bir Word belgesi nasıl düzenlenir] 26
  • [FileFormat.words kullanarak kelime dosyalarında tablo nasıl yapılır] 27