fileformat.words是一个开源库,提供了一个免费的文字处理器模块,该模块可以在编程中允许您在Word Documents中添加/修改表。

如何使用fileformat.words在Word文件中制作表格

概述

在数据表示方面,Word文档* *中的一个 表被视为不可或缺的部分。它是最常见的文档元素,在构建业务文档方面提供了极大的便利。令人惊讶的是,您可以在Word文档中创建一个表,而无需在本地计算机上使用或安装MS Word。是的,让我介绍一个 免费文字处理器 ,使您能够以编程方式创建和操纵Word文档。 .NET的FileFormat.Words是Word文档处理的完整软件包。因此,在此博客文章中,我们将使用此.NET库FileFormat.Words学习如何在Word中制作表。 该博客文章涵盖以下各节:

Word的表生成器 - API安装

.NET的FileFormat.Words为MS Word处理提供了广泛的功能。此开源API非常容易安装。但是,您可以通过将以下命令运行到Nuget Package Manager中下载安装的Nuget软件包

Install-Package FileFormat.Words

如何以编程方式在Word文档中创建表

让我们编写一些代码,以查看此开源 免费的Word Processo r。实际上,我们将看到如何使用fileformat.words库在Word文档中制作表。 请按照以下步骤和代码段:

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");
            }

        }

    }
}

上图显示了上述代码段的输出:

如何在Word文档中创建表

结论

这将我们带到了本文的结尾,我们希望您使用fileformat.words库在单词 文档中制作表。此外,如果您想在C#中构建用于Word文档的表生成器,则本指南将为您提供帮助。此外,此 免费文字处理器**API是开源的,您可以找到其文档此处。 最后,fileformat.com继续写有关其他主题的博客文章。此外,您可以在我们的社交媒体平台上关注我们,包括FacebookLinkedInTwitter

贡献

由于.NET的FileFormat.Words是一个开源项目,可在GitHub上找到。因此,社区的贡献非常感谢。

问一个问题

您可以在我们的论坛上让我们知道您的问题或查询。

常见问题 - 常见问题

如何在Word文档中创建表? 使用此开源.NET库在Word文档中创建表非常容易。此外,您可以探索此API 进一步如何在C#中创建DOCX文件? 请按照此链接进行详细的代码段,并在C#中创建DOCX文件的步骤。 如何在Word中创建自定义表格式? .NET的FileFormat.Words是一个免费的库,它提供了以编程方式操纵和创建MS Word的功能。实际上,您可以探索此名称空间FileFormat.Words.table以查看方法和属性。

另请参见