翻譯|使用教程|編輯:周思宇|2023-04-12 17:28:20.437|閱讀 250 次
概述:在本文中,我將向您展示使用 Java 從模板動態(tài)生成 Word 文檔的過程。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
報告生成過程通常包括填充包含所需字段占位符的預(yù)定義文檔模板。報告引擎將模板文檔作為輸入,用數(shù)據(jù)動態(tài)填充占位符并生成結(jié)果報告。本文還將展示一個類似的用例,您可以通過使用 Java 以編程方式動態(tài)填充 Word 模板來生成 Word 文檔。
要從 DOCX 模板生成 Word 文檔,我們將使用 Aspose.Words for Java API 提供的 LINQ Reporting Engine。 LINQ 報告引擎支持用于 Word 模板的文本、圖像、列表、表格、超鏈接和書簽的各種標(biāo)記。包含這些標(biāo)簽的模板文檔由引擎填充來自 Java 對象以及 XML、JSON 或 CSV 數(shù)據(jù)源的數(shù)據(jù)。因此,讓我們開始使用 Java 從模板生成 Word 文檔。
本文將介紹如何使用以下模板從模板生成 Word 文檔:
您可以下載 Aspose.Words for Java JAR 或使用以下配置將其添加到基于 Maven 的應(yīng)用程序中。
存儲庫:
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>//repository.aspose.com/repo/</url> </repository>依賴:
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>20.1</version> <type>pom</type> </dependency>
要開始生成報告,我們首先通過使用 Java 對象中的值填充模板來創(chuàng)建一個 Word 文檔。為了定義文檔模板,創(chuàng)建一個新的 Word 文檔,插入以下標(biāo)簽并將其保存為 DOCX 文檔。
<<[s.getName()]>> says: "<<[s.getMessage()]>>."
在上面的模板中,“s”將被視為 Java 類的對象,用于填充標(biāo)簽。因此,讓我們創(chuàng)建一個名為 Sender 的類,其中包含兩個數(shù)據(jù)成員。
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)在,是時候?qū)?Word 模板傳遞給 LINQ 報告引擎并根據(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");
現(xiàn)在讓我們更進(jì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>在這種情況下,我們將在模板文檔中為 XML 數(shù)據(jù)源中的多條記錄使用以下標(biāo)記。
<<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 對象作為數(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");
現(xiàn)在讓我們看看如何使用 JSON 數(shù)據(jù)源從 DOCX 模板生成 Word 文檔。以下是我們將在此示例中使用的 JSON 數(shù)據(jù)。
[ { Name: "John Smith", Contract: [ { Client: { Name: "A Company" }, Price: 1200000 }, { Client: { Name: "B Ltd." }, Price: 750000 }, { Client: { Name: "C & D" }, Price: 350000 } ] }, { Name: "Tony Anderson", Contract: [ { Client: { Name: "E Corp." }, Price: 650000 }, { Client: { Name: "F & Partners" }, Price: 550000 } ] }, ]在此示例中,我們將生成 Word 文檔,其中包含按經(jīng)理分組的客戶列表。根據(jù)這種情況,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");
最后但同樣重要的是,讓我們看看如何通過使用以下 CSV 數(shù)據(jù)源填充 Word 模板來生成 Word 文檔。
John Doe,30,1989-04-01 4:00:00 pm Jane Doe,27,1992-01-31 07:00:00 am John Smith,51,1968-03-08 1:00:00 pm本示例將使用以下 Word 模板:
<<foreach [in persons]>>Name: <<[Column1]>>, Age: <<[Column2]>>, Date of Birth: <<[Column3]:"dd.MM.yyyy">> <</foreach>> Average age: <<[persons.average(p => p.Column2)]>>為了處理 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");
以上便是如何使用 Java 從模板動態(tài)生成 Word 文檔的過程,要是您還有其他關(guān)于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn