Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Word文档模板C中填充表格#_C#_Templates_Ms Word_Populate - Fatal编程技术网

C# 在Word文档模板C中填充表格#

C# 在Word文档模板C中填充表格#,c#,templates,ms-word,populate,C#,Templates,Ms Word,Populate,有人知道打开word文档模板并通过C#编程在其中填充表格的好方法吗?如果是我,我会使用这个方法 最佳选项(至少对于docx格式)是 在下面的博文中,您可以找到将非常简单的文档操作与DocX、Microsoft的OOXML API和经典的Office互操作库进行比较的代码示例: 如果您对商业产品感兴趣并使用DOCX文件格式,您可以尝试我们的组件 它有自己的读/写引擎和简单的内容模型,可以在不安装MS Word的情况下使用 下面是一个示例C#代码,介绍如何创建一个简单的模板文档,其中包含一个表,该

有人知道打开word文档模板并通过C#编程在其中填充表格的好方法吗?

如果是我,我会使用这个方法

最佳选项(至少对于docx格式)是

在下面的博文中,您可以找到将非常简单的文档操作与DocX、Microsoft的OOXML API和经典的Office互操作库进行比较的代码示例:

如果您对商业产品感兴趣并使用DOCX文件格式,您可以尝试我们的组件

它有自己的读/写引擎和简单的内容模型,可以在不安装MS Word的情况下使用

下面是一个示例C#代码,介绍如何创建一个简单的模板文档,其中包含一个表,该表将使用邮件合并功能用数据展开:

// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data.
// You don't have to do this if you already have a DataTable instance.
var dataTable = new DataTable("People")
{
    Columns =
    {
        new DataColumn("Name", typeof(string)),
        new DataColumn("Surname", typeof(string))
    },
    Rows =
    {
        new object[] { "John", "Doe" },
        new object[] { "Fred", "Nurk" },
        new object[] { "Hans", "Meier" },
        new object[] { "Ivan", "Horvat" }
    }
};

// Create and save a template document. 
// You don't have to do this if you already have a template document.
// This code is only provided as a reference how template document should look like.
var document = new DocumentModel();
document.Sections.Add(
    new Section(document,
        new Table(document,
            new TableRow(document,
                new TableCell(document,
                    new Paragraph(document, "Name")),
                new TableCell(document,
                    new Paragraph(document, "Surname"))),
            new TableRow(document,
                new TableCell(document,
                    new Paragraph(document,
                        new Field(document, FieldType.MergeField, "RangeStart:People"),
                        new Field(document, FieldType.MergeField, "Name"))),
                new TableCell(document,
                    new Paragraph(document,
                        new Field(document, FieldType.MergeField, "Surname"),
                        new Field(document, FieldType.MergeField, "RangeEnd:People")))))));
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault);

// Load a template document.
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault);

// Mail merge template document with DataTable.
// Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match.
document.MailMerge.ExecuteRange(dataTable);

// Save the mail merged document.
document.Save("Document.docx", SaveOptions.DocxDefault);

我在模板文件中有预先制作的表格,我不确定docx是否有填充该预先制作表格的功能。当然,当您使用docx.Load函数读取现有文档时,生成的文档对象有一个表格列表,您可以使用该列表来操作单元格中的数据。这里有更多信息(用于创建表,但它显示了我正在谈论的对象)