ادغام افقی یا ادغام عمودی یک یا چند سلول جدول را در پرونده های DOCS/DOCX انجام دهید. FileFormat.Words روش هایی را برای کار با جداول در پرونده های Word فراهم می کند.

نحوه ادغام سلولهای جدول در اسناد کلمه

نمای کلی

نسخه جدیدتر [FileFormat.words] 2 روش های دیگری را برای کار با جداول در اسناد [Word] 3 ارائه می دهد. [نسخه] قبلی 4 شامل روش هایی برای ایجاد ، ویرایش و خواندن ویژگی های جدول است در حالی که آخرین نسخه به کاربران امکان می دهد سلولهای جدول را به صورت برنامه ای در پرونده های DOCS/DOCX ادغام کنند. علاوه بر این ، شما می توانید** ادغام افقی یا ادغام عمودی **سلولهای جدول را با استفاده از این منبع باز .NET [کتابخانه] 5 انجام دهید. علاوه بر این ، این یک کتابخانه آسان برای استفاده است که روش های آن پیچیده نیست و به وابستگی شخص ثالث احتیاج ندارد. در این پست وبلاگ ، ما می آموزیم که چگونه سلولهای جدول را در اسناد کلمه ادغام کنیم. بنابراین ، بیایید روند نصب را شروع کنیم و کد منبع را شروع کنیم. ما در این مقاله عناوین زیر را پوشش خواهیم داد:

  • [نصب API ژنراتور جدول] 6
  • [نحوه ادغام سلولهای جدول در اسناد کلمه] 7

نصب API ژنراتور جدول

لطفاً برای اطلاعات دقیق در مورد نصب ، به این [لینک] 8 مراجعه کنید. فقط برای استفاده مجدد ، فرآیند نصب این API .NET رایگان نسبتاً آسان است. خوب ، می توانید [بسته NuGet] 9 را بارگیری کنید یا دستور زیر را در مدیر بسته NUGET اجرا کنید:

Install-Package FileFormat.Words

نحوه ادغام سلولهای جدول در اسناد کلمه

ما یک قطعه کد برای دستیابی به ادغام افقی و ادغام عمودی سلولهای جدول خواهیم نوشت. برای این منظور ، ما از کلاس ها و روش های در معرض استفاده خواهیم کرد. لطفاً برای یادگیری نحوه ایجاد یک جدول در یک سند Word با استفاده از FileFormat.words به این راهنما مراجعه کنید. بنابراین ، ما از کلاس ها و روش های بیشتر در قطعه کد خود استفاده خواهیم کرد:

  • یک شیء از کلاس [VerticalMerge] 10 ایجاد کنید.
  • [ادغام] 11 از خاصیت استفاده می شود تا مشخص شود که این عنصر باید یک منطقه جدید ادغام شده عمودی را در جدول شروع کند.
  • از روش [APPEND] 12 برای اتصال شیء VerticalMerge با شی TBLCellProps استفاده کنید.
  • نمونه ای از کلاس [HorizontalMerge] 13 را فوری کنید.
  • [ادغام] 14 از خاصیت استفاده می شود تا مشخص شود که این عنصر باید یک منطقه جدید ادغام شده افقی را در جدول شروع کند.
  • با روش [APPEND] 15 تماس بگیرید تا شیء HorizontalMerge را با شی 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 اسناد به صورت برنامه ای ادغام کنید. علاوه بر این ، ما همچنین کد منبع را برای اجرای ادغام افقی و ادغام عمودی سلولهای جدول نوشتیم. بنابراین ، شما ممکن است برای اتوماسیون اتوماسیون فایل Word ، از این منبع باز استفاده کنید. در پایان ، برای یادگیری در مورد کلاس ها و روش های بیشتر ، به بازدید از [مستندات] 19 فراموش نکنید. سرانجام ، [FileFormat.com] 20 به طور مداوم در حال نوشتن پست های وبلاگ آموزشی در موضوعات جالب است. بنابراین ، لطفاً برای به روزرسانی های منظم در تماس باشید. علاوه بر این ، شما می توانید ما را در سیستم عامل های رسانه های اجتماعی ما ، از جمله [Facebook] 21 ، [LinkedIn] 22 و [Twitter] 23 دنبال کنید.

مشارکت

از آنجا که [FileFormat.Words for .NET] 5 یک پروژه منبع باز است و در [GitHub] 24 در دسترس است. بنابراین ، از سهم جامعه بسیار استقبال می شود.

سوالی بپرسید.

شما می توانید در مورد سؤالات یا سؤالات خود در مورد [انجمن] 25 به ما اطلاع دهید.

سؤالات متداول

چگونه می توانم سلول ها را در یک سند کلمه ادغام کنم؟ شما می توانید سلولهای جدول را با استفاده از کلاسهای [VerticalMerge] 10 و [HorizontalMerge] 13 ادغام کنید. چگونه می توانم سلول ها را در یک جدول در Doc ادغام کنم؟ لطفاً برای یادگیری مراحل و قطعه کد برای دستیابی به این قابلیت ، این [پیوند] 7 را دنبال کنید.

همچنین {.wp-block-heading} را ببینید

  • [نحوه ایجاد یک سند Word در C# با استفاده از FileFormat.Words] 8
  • [نحوه ویرایش یک سند Word در C# با استفاده از FileFormat.Words] 26
  • [نحوه تهیه جدول در پرونده های Word با استفاده از FileFormat.Words] 27