【问题标题】:Populate Table In Word Document Template C#在 Word 文档模板 C# 中填充表格
【发布时间】:2012-03-27 22:32:38
【问题描述】:

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

【问题讨论】:

    标签: c# templates ms-word populate


    【解决方案1】:

    如果是我,我会用这个

    DOCX

    【讨论】:

      【解决方案2】:

      最好的选择(至少对于 docx 格式)是 http://docx.codeplex.com/

      在下面的博客文章中,您可以找到代码示例,将非常简单的文档操作与 DocX、Microsoft 的 OOXML API 和经典的 Office 互操作库进行比较: http://cathalscorner.blogspot.com/2010/06/cathal-why-did-you-create-docx.html

      【讨论】:

      • 我在模板文件中有预制表格,我不确定 docx 是否具有填充该预制表格的功能。
      • 绝对,当您使用 DocX.Load 函数读取现有文档时,生成的文档对象有一个表格列表,您可以使用它来操作单元格中的数据。更多信息在这里(它用于创建表格,但它显示了我正在谈论的对象)cathalscorner.blogspot.com/2010/06/docx-and-tables.html
      【解决方案3】:

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

      它有自己的读/写引擎和简单的内容模型,可以在没有安装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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多