翻譯|使用教程|編輯:李顯亮|2020-01-15 10:49:01.467|閱讀 711 次
概述:本文將展示一個(gè)類似的用例,在這個(gè)用例中,可以使用Java動(dòng)態(tài)和程序化地填充Word模板來生成Word文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
報(bào)告生成過程通常包括填充包含所需字段占位符的預(yù)定義文檔模板。報(bào)告引擎將模板文檔作為輸入,動(dòng)態(tài)地用數(shù)據(jù)填充占位符并生成結(jié)果報(bào)告。在Aspose.Words for Java 2020首更中,增加了對LINQ Reporting Engine的動(dòng)態(tài)書簽插入功能的支持。
LINQ Reporting Engine支持各種用于Word模板的文本,圖像,列表,表格,超鏈接和書簽的標(biāo)簽。引擎將使用來自Java對象以及XML,JSON或CSV數(shù)據(jù)源的數(shù)據(jù)填充包含這些標(biāo)簽的模板文檔。這樣就能夠輕松的實(shí)現(xiàn)報(bào)告生成的功能。
本文將展示一個(gè)類似的用例,在這個(gè)用例中,可以使用Java動(dòng)態(tài)和程序化地填充Word模板來生成Word文檔。如果想要測試這項(xiàng)新功能,可點(diǎn)擊此處下載最新版試用。
(本文篇幅略長,建議收藏閱讀)
本文將介紹如何使用以下方法從模板生成Word文檔:
首先通過使用Java對象中的值填充模板來創(chuàng)建Word文檔。為了定義文檔模板,創(chuàng)建一個(gè)新的Word文檔,插入以下標(biāo)簽并將其另存為DOCX文檔。
<<[s.getName()]>> says: "<<[s.getMessage()]>>."
在上面的模板中,“ s ”將被視為Java類的一個(gè)對象,該對象將用于填充標(biāo)簽。因此,讓我們創(chuàng)建一個(gè)具有兩個(gè)數(shù)據(jù)成員的名為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; }
現(xiàn)在,是時(shí)候?qū)ord模板傳遞給LINQ Reporting Engine并根據(jù)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");
輸出結(jié)果:
接下來,看看如何在一個(gè)有點(diǎn)復(fù)雜的場景中使用XML數(shù)據(jù)源填充Word模板。以下是我們將用來填充Word模板的XML數(shù)據(jù)源。
<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>
在這種情況下,我們將在模板文檔中將以下標(biāo)記用于XML數(shù)據(jù)源中的多個(gè)記錄。
<<foreach [in persons]>>
<
<> Average age: <<[persons.average(p => p.Age)]>>
在這種情況下,用于生成Word文檔的Java代碼是相同的,除了將Java對象作為數(shù)據(jù)源傳遞外,我們將在ReportingEngine.buildReport()方法中傳遞XmlDataSource對象。下面的代碼示例演示如何通過使用Java中的XML數(shù)據(jù)源填充文檔模板來創(chuàng)建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");
輸出結(jié)果:
現(xiàn)在讓我們看一下如何使用JSON數(shù)據(jù)源從DOCX模板生成Word文檔。以下是在此示例中將使用的JSON數(shù)據(jù)。
在此示例中,我們將生成Word文檔,其中包含按其經(jīng)理分組的客戶列表。按照這種情況,DOCX模板將如下所示:
<<foreach [in managers]>>Manager: <<[Name]>> Contracts: <<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>) <</foreach>> <</foreach>>
為了加載JSON數(shù)據(jù)源,Aspose.Words提供了JsonDataSource類。以下代碼示例演示如何使用Java中的JSON數(shù)據(jù)源從模板創(chuàng)建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");
輸出結(jié)果:
最后但并非最不重要的一點(diǎn),檢查如何通過使用以下CSV數(shù)據(jù)源填充Word模板來生成Word文檔。
為了處理CSV數(shù)據(jù)源,Aspose.Words提供了CsvDataSource類。下面的代碼示例演示如何使用Java中的CSV數(shù)據(jù)源生成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");
輸出結(jié)果:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn