##이 블로그 게시물을 따라 프로그래밍 방식으로 워드 문서에 테이블 헤더를 추가하는 방법을 알아보십시오. Fileformat.words는 풍부한 테이블 생성 및 조작 방법을 제공합니다. {.WP- 블록 헤드}

단어 문서에 테이블 헤더를 삽입하는 방법

개요

데이터 테이블은 MS Word 문서의 중요한 요소입니다. 테이블로 작업하는 것은 일상적인 작업이지만 여러 데이터 테이블이 포함 된 여러 문서가 있으면 어떻게해야합니까? 물론, 반복적 인 작업을 자동화하여 시간을 절약하고 생산성을 높이려면 일종의 자동화가 필요합니다. 따라서 fileformat.words는 단어 생성, 수정 및 처리를 자동화하기위한 오픈 소스 .NET 라이브러리입니다. 이 기사에서는이 C# API를 사용하여 Word 문서에 테이블 헤더를 삽입하는 방법을 살펴 봅니다. 그러나 MS Word 테이블과 관련된 다양한 주제에 대해서는 이전 기사를 방문 할 수 있습니다. 이 블로그 게시물에서 다음 섹션을 살펴 보겠습니다.

테이블 헤더 작업 - API 설치

.net 라이브러리의 Fileformat.words의 설치 절차는 몇 초 만에 문제입니다. 이 엔터프라이즈 수준의 .NET API는 사용자가 활용할 수있는 방대한 기능을 제공합니다. 따라서 Nuget 패키지을 다운로드하거나 Nuget 패키지 관리자에서 다음 명령을 실행할 수 있습니다.

Install-Package FileFormat.Words

프로그래밍 방식으로 Word 파일에 테이블 헤더 추가

설치가 완료되며 다음 단계는 코드 스 니펫을 즉시 작성하는 것입니다. 또한 Word 문서에서 테이블을 만들 수있을뿐만 아니라 프로그래밍 방식으로 테이블의 레이아웃을 사용자 정의 할 수 있습니다. 아래에 언급 된 단계와 코드 스 니펫을 따를 수 있습니다.

  • 문서 클래스의 객체를 인스턴스화하십시오.
  • 문서 클래스 객체로 body 클래스의 생성자를 초기화하십시오.
  • 클래스의 인스턴스를 만듭니다.
  • 테이블 헤더 메소드를 호출하여 첫 ​​번째 열의 헤더를 설정하십시오.
  • Append 메소드를 호출하여 행을 테이블에 추가하십시오.
  • 부록 메소드를 호출하여 문서 본문에 테이블을 추가하십시오.
  • 저장 메소드는 Word 문서를 디스크에 저장합니다.
using FileFormat.Words;
using FileFormat.Words.Table;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instantiate an object of the Document class.
            using (Document doc = new Document())
            {
                // Initialize the constructor of the Body class with the Document class object.
                Body body = new Body(doc);
                // Create an instance 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 dashed_border method to set the border style and border line width.
                topBorder.dashed_border(20);
                // To set the border of the bottom side of the table.
                BottomBorder bottomBorder = new BottomBorder();
                bottomBorder.dashed_border(20);
                // To set the border of the right side of the table.
                RightBorder rightBorder = new RightBorder();
                rightBorder.dashed_border(20);
                // To set the border of the left side of the table.
                LeftBorder leftBorder = new LeftBorder();
                leftBorder.dashed_border(20);
                // To set the inside vertical border of the table.
                InsideVerticalBorder insideVerticalBorder = new InsideVerticalBorder();
                insideVerticalBorder.dashed_border(20);
                // To set the inside vehorizontalrtical border of the table.
                InsideHorizontalBorder insideHorizontalBorder = new InsideHorizontalBorder();
                insideHorizontalBorder.dashed_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 propertiese 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 istance 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("Country");
                run.Text = "England";
                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();

                // Invoke the TableHeaders method to set the header of the second column
                table.TableHeaders("Capital");
                run2.Text = "London";
                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();
                table.TableHeaders("Population");
                run3.Text = "1000000";
                para3.AppendChild(run3);
                tableCell3.Append(para3);

                TableCellProperties tblCellProps3 = new TableCellProperties();
                tblCellProps3.Append(new TableCellWidth("1400"));
                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 = "Pakistan";
                _para.AppendChild(_run);
                _tableCell.Append(_para);

                TableCellProperties tblCellProps1_ = new TableCellProperties();
                tblCellProps1_.Append(new TableCellWidth("2400"));
                _tableCell.Append(tblCellProps1_);


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

                _run2.Text = "Islamabad";
                _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 = "2000000";
                _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);
                // Invoke the Append method to add the rows into table.
                table.Append(tableRow);
                table.Append(tableRow2);
                // Call the AppendChild method to 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");
            }

        }

    }
}

위의 코드를 기본 파일에 복사하여 붙여 넣고 프로그램을 실행하십시오. 아래 이미지에 표시된 출력이 표시됩니다.

MS Word의 테이블

결론 {.WP- 블록 헤드}

Fileformat.words 라이브러리를 사용하여 Word 문서에 테이블 헤더를 삽입하는 방법을 배웠기를 희망 하면서이 블로그 게시물을 끝내고 있습니다. 또한 설치 프로세스와 코드 스 니펫도 겪었습니다. 또한 문서에서 탐색 할 수있는 다른 실용적인 방법이 있습니다. 마지막으로, fileformat.com는 다른 주제에 대한 블로그 게시물을 계속 작성합니다. 또한 Facebook, LinkedInTwitter를 포함한 소셜 미디어 플랫폼에서 우리를 따라갈 수 있습니다.

기부 {.WP- 블록 헤드}

fileformat.words for .net는 오픈 소스 프로젝트이며 github에서 사용할 수 있습니다. 따라서 커뮤니티의 기여는 대단히 감사합니다.

질문

포럼에서 귀하의 질문이나 질문에 대해 알려줄 수 있습니다.

자주 묻는 질문-FAQ

** 헤더가있는 테이블을 어떻게 삽입합니까?** C#에 테이블 헤더를 삽입하는 방법을 알아 보려면이 링크를 따라 가십시오.

{.WP- 블록 헤드} 참조