有谁知道打开Word文档模板并通过C#编程填充表格的好方法吗?在Word文档模板中填充表格C#
2
A
回答
3
如果是我,这是什么,我会用
2
最好的选择(为DOCX格式至少)是 http://docx.codeplex.com/
在下面的博客文章,你可以找到的代码示例比较DocX,Microsoft的OOXML API和经典Office Interop库的非常简单的文档操作: http://cathalscorner.blogspot.com/2010/06/cathal-why-did-you-create-docx.html
0
如果您对商业产品感兴趣并使用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);
相关问题
- 1. 在word文档中填充表格单元格颜色的C#代码
- 2. Word模板,填充字段,C#.NET
- 3. 访问和填充Word 2007文档模板使用C#和Microsoft.Office.Interop.Word快速部件?
- 4. 从cml中填充word文档从xml#
- 5. 如何在ASP.NET中使用Visual Basic/Visual C#填充Word文档
- 6. 在模板中填充rootURL
- 7. 在网格模板列中填充DropDownList列表
- 8. 在django模板中使用knockout.js填充表格
- 9. 在asp.net中填写word文档?
- 10. .net模板引擎生成word文档
- 11. 文档模板:用JavaScript代替Word?
- 12. Word 2003模板 - 文档保存
- 13. 在Word文档中编辑Excel电子表格对象(C#Interop)
- 14. c#如何在word文档的页脚中获取表格
- 15. C#在word文档中动态添加行到表格
- 16. 从SharePoint自定义列表的列表字段中自动填充Word模板?
- 17. 从excel vba中心表格Word文档
- 18. rmarkdown word文档中的HTML格式表
- 19. 从Excel中填充Word文档而不删除书签
- 20. 在C编辑Word模板#
- 21. 比较文档模板Word文档文件(.docx)(.DOTX)
- 22. 使用MS Access表中的数据填充MS Word表格
- 23. C#使用OpenXml填充单词模板
- 24. 在c#中使用word interop在word表格中添加文本
- 25. C#Word文档 - 如何清除格式?
- 26. Ember.js - 在表格(车把模板)中填充模型时使用自动索引
- 27. 在C#中打开空格在路径中的Word文档
- 28. 使用Apache Poi解析Word文档中表格中的表格
- 29. 基于单词模板在asp.net中创建word文档
- 30. 如何在word文档中设置页边距和模板?
在模板文件我已经预先制作的表,我不知道docx有填充预制表的功能。 – user1261700 2012-03-11 06:39:51
绝对,当您使用DocX.Load函数读取现有文档时,生成的文档对象具有可用于处理单元格中的数据的Tables列表。更多信息(这是为了创建表格,但它显示了我正在谈论的对象)http://cathalscorner.blogspot.com/2010/06/docx-and-tables.html – Dirk 2012-03-11 21:44:35