FileFormat.words là một thư viện nguồn mở cung cấp mô-đun bộ xử lý Word miễn phí để lập trình cho phép bạn thêm/sửa đổi các bảng trong các tài liệu Word.

Cách tạo bảng trong các tệp Word bằng FileFormat.words

Tổng quan

Một bảng trong một tài liệu Word được coi là một phần không thể thiếu khi nói đến biểu diễn dữ liệu. Đây là yếu tố tài liệu phổ biến nhất và cung cấp sự tiện lợi tuyệt vời về mặt xây dựng tài liệu kinh doanh. Thật đáng ngạc nhiên, bạn có thể tạo một bảng trong tài liệu Word mà không cần sử dụng hoặc cài đặt MS Word trên máy cục bộ của bạn. Có, hãy để tôi giới thiệu một Trình xử lý văn bản miễn phí cho phép bạn tạo và thao tác các tài liệu từ được lập trình. FileFormat.words cho .NET là một gói hoàn chỉnh của xử lý tài liệu Word. Do đó, trong bài đăng trên blog này, chúng tôi sẽ tìm hiểu cách tạo một bảng trong Word tài liệu bằng thư viện .NET này FileFormat.words. Bài đăng trên blog này bao gồm các phần sau:

Trình tạo bảng cho Word - Cài đặt API

FileFormat.words cho .NET cung cấp một loạt các tính năng để xử lý từ MS. API nguồn mở này rất dễ cài đặt. Tuy nhiên, bạn có thể tải xuống Gói Nuget cài đặt nó bằng cách chạy lệnh sau vào Trình quản lý gói Nuget.

Install-Package FileFormat.Words

Cách tạo bảng trong tài liệu Word theo chương trình

Hãy để viết một số mã để xem phần nguồn mở này Word Processo r đang hoạt động. Trên thực tế, chúng ta sẽ thấy cách tạo một bảng trong một tài liệu Word bằng thư viện FileFormat.words. Vui lòng làm theo các bước sau và đoạn mã:

  • Khởi tạo một thể hiện của lớp tài liệu.
  • Khởi tạo hàm tạo của lớp cơ thể với đối tượng lớp tài liệu.
  • Tạo một đối tượng của lớp Bảng.
  • Khởi tạo các hàm tạo của topborder, [chai Bottombord Đặt đường viền của tất cả các cạnh của bảng.
  • Gọi phương thức basicblacksquares_border để đặt kiểu biên giới và chiều rộng biên giới.
  • Tạo một thể hiện của lớp TableBorder.
  • Append Các đối tượng của topborder, chai Bottomborder, Rightborder, Leftbandder, InsidePverTicalBorder và InsideHorizontalborder Lớp đến đối tượng của lớp TableBorder.
  • Khởi tạo một thể hiện của lớp TableProperies.
  • Gọi phương thức append của lớp TableProperIES để đính kèm đối tượng của lớp bảng thứ tự.
  • Tạo một thể hiện của lớp TableJustification và gọi phương thức Alignleft để định vị bảng ở phía bên trái của tài liệu.
  • Gọi phương thức append để đính kèm đối tượng TableJustification vào đối tượng TBLProp.
  • Phương thức Phụ lục của lớp bảng sẽ gắn các thuộc tính bảng vào bảng.
  • Tạo một đối tượng của lớp Tablerow để tạo một hàng bảng.
  • Khởi tạo một thể hiện của lớp TableCell.
  • Đặt tiêu đề của cột đầu tiên bằng cách gọi phương thức TableHeaders.
  • Gọi phương thức append của lớp TableCell để thêm văn bản bên trong ô bảng.
  • Tạo một đối tượng của TableCellProperIES Thuộc tính bảng
  • Đặt chiều rộng của ô bảng bằng cách khởi tạo đối tượng của lớp TableCellWidth và nối vào đối tượng TBLCellProps.
  • Phương thức append sẽ đính kèm đối tượng TBLCellProps với đối tượng của lớp TableCell.
  • Gọi phương thức append để thêm các hàng vào bảng.
  • Phương thức Phụ lục sẽ thêm bảng vào phần thân của tài liệu.
  • Phương thức lưu sẽ lưu tài liệu từ vào đĩa.
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 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("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();
                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);

                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 = "sultan";
                _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 = "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("/Docs.docx");
            }

        }

    }
}

Đầu ra của đoạn mã trên được hiển thị trong hình dưới đây:

Cách tạo bảng trong tài liệu Word

Kết luận

Điều này đưa chúng ta đến cuối bài viết này và chúng tôi hy vọng bạn đã học được Cách tạo một bảng trong một tài liệu bằng cách sử dụng thư viện FileFormat.words. Ngoài ra, hướng dẫn này sẽ giúp bạn nếu bạn đang tìm cách xây dựng một trình tạo bảng cho các tài liệu từ trong C#. Hơn nữa, bộ xử lý văn bản miễn phí này**API là nguồn mở và bạn có thể tìm thấy tài liệu của nó ở đây. Cuối cùng, FileFormat.com tiếp tục viết các bài đăng trên blog về các chủ đề khác. Hơn nữa, bạn có thể theo dõi chúng tôi trên các nền tảng truyền thông xã hội của chúng tôi, bao gồm Facebook, LinkedIn, và Twitter.

Đóng góp

FileFormat.words cho .NET là một dự án nguồn mở và có sẵn trên GitHub. Vì vậy, sự đóng góp từ cộng đồng được đánh giá cao.

Đặt câu hỏi

Bạn có thể cho chúng tôi biết về câu hỏi hoặc truy vấn của bạn trên [Diễn đàn] của chúng tôi 43.

Câu hỏi thường gặp-Câu hỏi thường gặp

** Làm cách nào để tạo bảng trong tài liệu Word?** Rất dễ dàng để tạo một bảng trong tài liệu Word bằng nguồn mở này thư viện .NET. Hơn nữa, bạn có thể khám phá API này hơn nữa. ** Cách tạo tệp docx trong c#?** Vui lòng theo dõi Liên kết để xem qua đoạn mã chi tiết và các bước để tạo tệp Docx trong C#. ** Làm cách nào để tạo định dạng bảng tùy chỉnh trong Word?** FileFormat.words cho .NET là một thư viện miễn phí cung cấp các tính năng để thao tác và tạo MS Word lập trình. Trên thực tế, bạn có thể khám phá không gian tên này fileformat.words.table để xem các phương thức và thuộc tính.

Xem thêm