.NET版Word处理控件Aspose.Words功能演示:从C#.NET中的模板生成Word文档
Word文档的自动生成已被企业广泛用于创建大量报告。在某些情况下,文档是从头开始创建的。另一方面,预定义的模板用于通过填充占位符来生成Word文档。
在本文中,我将演示如何在C#中以动态方式和编程方式从模板生成Word文档。将了解如何从不同类型的数据源填充Word模板。本文将介绍以下方案以及代码示例:
- 使用C#对象的值从模板生成Word文档。
- 使用XML数据源生成Word文档。
- 使用JSON数据源生成Word文档。
- 使用CSV数据源生成Word文档。
Aspose.Words for .NET已升级至V20.3,如果你还没有用过Aspose.Words可以点击这里下载最新版测试。
使用C#对象从模板生成Word文档
首先,让我们看看如何使用C#对象填充Word模板。为此,我们将创建一个Word文档(DOC / DOCX),该文档具有以下占位符作为文档的内容:
<<[sender.Name]>> says: "<<[sender.Message]>>."
在这里,发送者是以下类的对象,我们将使用该类来填充模板。
public class Sender
{
public string Name { get; set; }
public string Message { get; set; }
public Sender (string _name, string _message)
{
Name = _name;
Message = _message;
}
}
现在,我们将使用Aspose.Words的报告引擎,按照以下步骤从Sender类的模板和对象生成Word文档。
- 创建Document类的对象,并使用Word模板的路径对其进行初始化。
- 创建并初始化Sender类的对象。
- 创建ReportingEngine类的对象。
- 使用ReportingEngine.BuildReport()填充模板,该模板将Document的对象,数据源和数据源的名称作为参数。
- 使用Document.Save()方法保存生成的Word文档。
下面的代码示例演示如何从C#中的模板生成Word文档。
Document doc = new Document("template.docx");
Sender sender = new Sender("LINQ Reporting Engine", "Hello World");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, sender, "sender");
doc.Save("word.docx");
输出结果
从C#中的XML数据源生成Word文档
为了从XML数据源生成Word文档,我们将使用带有以下占位符的更为复杂的Word模板:
<>Name: <<[Name]>>, Age: <<[Age]>>, Date of Birth: <<[Birth]:"dd.MM.yyyy">> < > Average age: <<[persons.Average(p => p.Age)]>>
下面给出了我在此示例中使用的XML数据源。
| <Persons> | |
| <Person> | |
| <Name>John DoeName> | |
| <Age>30Age> | |
| <Birth>1989-04-01 4:00:00 pmBirth> | |
| Person> | |
| <Person> | |
| <Name>Jane DoeName> | |
| <Age>27Age> | |
| <Birth>1992-01-31 07:00:00 amBirth> | |
| Person> | |
| <Person> | |
| <Name>John SmithName> | |
| <Age>51Age> | |
| <Birth>1968-03-08 1:00:00 pmBirth> | |
| Person> | |
| Persons> |
以下是从XML数据源生成Word文档的步骤:
- 创建Document类的实例,并使用Word模板的路径对其进行初始化。
- 创建XmlDataSource类的实例,并使用XML文件的路径对其进行初始化。
- 创建ReportingEngine类的实例。
- 以与之前填充Word模板相同的方式使用ReportingEngine.BuildReport()方法。
- 使用Document.Save()方法保存生成的Word文档。
下面的代码示例演示如何从C#中的XML数据源生成Word文档。
Document doc = new Document("template.docx");
XmlDataSource dataSource = new XmlDataSource("datasource.xml");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dataSource, "persons");
doc.Save("word.docx");
输出结果
在C#中从JSON数据源生成Word文档
现在,让我们看看如何使用JSON数据源生成Word文档。在此示例中,我们将生成按其经理分组的客户列表。在这种情况下,以下为Word模板:
<>Manager: <<[Name]>> Contracts: < >- <<[Client.Name]>> ($<<[Price]>>) < > <>
以下是我们将用来填充模板的JSON数据源:
| [ | |
| { | |
| 姓名:“约翰史密斯”, | |
| 合同: | |
| [ | |
| { | |
| 客户: | |
| { | |
| 姓名:“一家公司” | |
| }, | |
| 价格:1200000 | |
| }, | |
| { | |
| 客户: | |
| { | |
| 姓名:“ B有限公司” | |
| }, | |
| 价钱:750000 | |
| }, | |
| { | |
| 客户: | |
| { | |
| 姓名:“ C&D ” | |
| }, | |
| 价钱:350000 | |
| } | |
| ] | |
| }, | |
| { | |
| 姓名:“托尼安德森”, | |
| 合同: | |
| [ | |
| { | |
| 客户: | |
| { | |
| 名称:“ E Corp. ” | |
| }, | |
| 价钱:650000 | |
| }, | |
| { | |
| 客户: | |
| { | |
| 姓名:“ F&Partners ” | |
| }, | |
| 价钱:550000 | |
| } | |
| ] | |
| }, | |
| ] |
以下是从XML数据源生成Word文档的步骤:
- 创建Document类的实例,并使用Word模板的路径对其进行初始化。
- 创建XmlDataSource类的实例,并使用XML文件的路径对其进行初始化。
- 创建ReportingEngine类的实例。
- 以与之前填充Word模板相同的方式使用ReportingEngine.BuildReport()方法。
- 使用Document.Save()方法保存生成的Word文档。
为了从JSON生成Word文档,我们将使用JsonDataSource类加载和使用JSON数据源,其余步骤将保持不变。下面的代码示例演示如何使用C#中的JSON从模板生成Word文档。
Document doc = new Document("template.docx");
JsonDataSource dataSource = new JsonDataSource("datasource.json");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dataSource, "managers");
doc.Save("word.docx");
输出结果
在C#中从CSV数据源生成Word文档
为了从CSV生成Word文档,我们将使用以下Word模板:
<>Name: <<[Column1]>>, Age: <<[Column2]>>, Date of Birth: <<[Column3]:"dd.MM.yyyy">> < > Average age: <<[persons.Average(p => p.Column2)]>>
下面给出了我在此示例中使用的XML数据源。

现在,让我们来看一下C#代码。除了一个更改,我们将使用CsvDataSource类加载CSV数据,此处所有步骤都将保持不变。下面的代码示例演示如何从CSV数据源生成Word文档。
Document doc = new Document("template.docx");
CsvDataSource dataSource = new CsvDataSource("datasource.csv");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dataSource, "persons");
doc.Save("word.docx");
输出结果

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线