翻譯|使用教程|編輯:李顯亮|2020-01-15 10:49:01.467|閱讀 711 次
概述:本文將展示一個類似的用例,在這個用例中,可以使用Java動態和程序化地填充Word模板來生成Word文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
報告生成過程通常包括填充包含所需字段占位符的預定義文檔模板。報告引擎將模板文檔作為輸入,動態地用數據填充占位符并生成結果報告。在Aspose.Words for Java 2020首更中,增加了對LINQ Reporting Engine的動態書簽插入功能的支持。
LINQ Reporting Engine支持各種用于Word模板的文本,圖像,列表,表格,超鏈接和書簽的標簽。引擎將使用來自Java對象以及XML,JSON或CSV數據源的數據填充包含這些標簽的模板文檔。這樣就能夠輕松的實現報告生成的功能。
本文將展示一個類似的用例,在這個用例中,可以使用Java動態和程序化地填充Word模板來生成Word文檔。如果想要測試這項新功能,可點擊此處下載最新版試用。
(本文篇幅略長,建議收藏閱讀)
本文將介紹如何使用以下方法從模板生成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文檔的步驟:
下面的代碼示例演示如何使用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模板。以下是我們將用來填充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]>>
<
<> 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數據源從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");
輸出結果:
最后但并非最不重要的一點,檢查如何通過使用以下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");
輸出結果:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn