报告生成过程通常包括填充包含所需字段占位符的预定义文档模板。报告引擎将模板文档作为输入,动态地用数据填充占位符并生成结果报告。在Aspose.Words for Java2020首更中,增加了对LINQ Reporting Engine的动态书签插入功能的支持。
LINQ Reporting Engine支持各种用于Word模板的文本,图像,列表,表格,超链接和书签的标签。引擎将使用来自Java对象以及XML,JSON或CSV数据源的数据填充包含这些标签的模板文档。这样就能够轻松的实现报告生成的功能。
本文将展示一个类似的用例,在这个用例中,可以使用Java动态和程序化地填充Word模板来生成Word文档。
(本文篇幅略长,建议收藏阅读)
本文将介绍如何使用以下方法从模板生成Word文档:
- 来自Java对象的值
- XML数据源
- JSON数据源
- CSV数据源
①使用Java对象从模板生成Word文档
首先通过使用Java对象中的值填充模板来创建Word文档。为了定义文档模板,创建一个新的Word文档,插入以下标签并将其另存为DOCX文档。
<<[s.getName()]>> says: "<<[s.getMessage()]>>."
在上面的模板中,“ s ”将被视为Java类的一个对象,该对象将用于填充标签。因此,让我们创建一个具有两个数据成员的名为Sender的类。
public class Sender { public Sender(String name, String message) { _name = name; _message = message; } public String getName() { return _name; } public String getMessage() { return _message; } private String _name; private String _message; }
现在,是时候将Word模板传递给LINQ Reporting Engine并根据Sender对象的值生成Word文档了。以下是生成Word文档的步骤:
- 创建Document对象,并使用模板的路径对其进行初始化
- 创建一个Sender类的对象,并使用值对其进行初始化
- 创建一个ReportingEngine对象
- 使用ReportingEngine.buildReport()方法生成报告,该方法接受文档模板,数据源和数据源的名称
- 使用Document.save()方法保存生成的Word文档
下面的代码示例演示如何使用Java对象的值从Word模板生成Word文档。
// Create Document object and initialize with DOCX template. Document doc = new Document("template.docx"); // Create Sender object. Sender sender = new Sender("LINQ Reporting Engine", "Hello World"); // Create ReportingEngine object. ReportingEngine engine = new ReportingEngine(); // Build report. engine.buildReport(doc, sender, "s"); // Save as Word document. doc.save("word.docx");
输出结果:
②使用XML数据从模板生成Word文档
接下来,看看如何在一个有点复杂的场景中使用XML数据源填充Word模板。以下是我们将用来填充Word模板的XML数据源。
<Persons> <Person> <Name>John Doe</Name> <Age>30</Age> <Birth>1989-04-01 4:00:00 pm</Birth> </Person> <Person> <Name>Jane Doe</Name> <Age>27</Age> <Birth>1992-01-31 07:00:00 am</Birth> </Person> <Person> <Name>John Smith</Name> <Age>51</Age> <Birth>1968-03-08 1:00:00 pm</Birth> </Person> </Persons>
在这种情况下,我们将在模板文档中将以下标记用于XML数据源中的多个记录。
<<foreach [in persons]>> <>Name: <<[Name]>>, Age: <<[Age]>>, Date of Birth: <<[Birth]:"dd.MM.yyyy">>
<</foreach>>
<> Average age: <<[persons.average(p => p.Age)]>>
在这种情况下,用于生成Word文档的Java代码是相同的,除了将Java对象作为数据源传递外,我们将在ReportingEngine.buildReport()方法中传递XmlDataSource对象。下面的代码示例演示如何通过使用Java中的XML数据源填充文档模板来创建Word文档。
// Create Document object and initialize with DOCX template. Document doc = new Document("template.docx"); // Load XML XmlDataSource dataSource = new XmlDataSource("./datasource.xml"); // Create ReportingEngine object. ReportingEngine engine = new ReportingEngine(); // Build report. engine.buildReport(doc, dataSource, "persons"); // Save as Word document. doc.save("word.docx");
输出结果:
③使用JSON数据从模板生成Word文档
现在让我们看一下如何使用JSON数据源从DOCX模板生成Word文档。以下是在此示例中将使用的JSON数据。
在此示例中,我们将生成Word文档,其中包含按其经理分组的客户列表。按照这种情况,DOCX模板将如下所示:
<<foreach [in managers]>>Manager: <<[Name]>> Contracts: <<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>) <</foreach>> <</foreach>>
为了加载JSON数据源,Aspose.Words提供了JsonDataSource类。以下代码示例演示如何使用Java中的JSON数据源从模板创建Word文档。
// Create Document object and initialize with DOCX template. Document doc = new Document("template.docx"); // Load JSON JsonDataSource dataSource = new JsonDataSource("datasource.json"); // Create ReportingEngine object. ReportingEngine engine = new ReportingEngine(); // Build report. engine.buildReport(doc, dataSource, "managers"); // Save as Word document. doc.save("word.docx");
输出结果:
④使用JSON数据从模板生成Word文档
最后但并非最不重要的一点,检查如何通过使用以下CSV数据源填充Word模板来生成Word文档。
为了处理CSV数据源,Aspose.Words提供了CsvDataSource类。下面的代码示例演示如何使用Java中的CSV数据源生成Word文档。
// Create Document object and initialize with DOCX template. Document doc = new Document("template.docx"); // Load CSV CsvDataSource dataSource = new CsvDataSource("datasource.csv"); // Create ReportingEngine object. ReportingEngine engine = new ReportingEngine(); // Build report. engine.buildReport(doc, dataSource, "persons"); // Save as Word document. doc.save("word.docx");
输出结果: