標籤:color catch template 電動 驗證 temp 文檔 定義 nbsp
一,建立word模板
1.建立一個word文檔
2.分別給四個參數設定域
(1)將滑鼠置於想要設定域的地方
(2)設定網域名稱
(3)設定好之後如所示
二,項目
1,引入maven依賴
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>18.5</version> <classifier>jdk16</classifier> </dependency>
2,載入授權檔案
public static boolean getLicense() { boolean result = false; try { InputStream is = AsposeToWordTest.class.getClassLoader().getResourceAsStream("license-word.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
3,擷取值以及插入到模板中並產生新的文檔
public static void main(String[] args) throws Exception { // 驗證License if (!getLicense()) { return; } //模板word String template = "E:\\test\\temp.docx"; //目標word String destdoc = "E:\\test\\edit.docx"; //定義文檔介面 Document doc = new Document(template); //文本域 String[] Flds = new String[]{"caseIssue","policeName", "caseName", "caseTime"}; String caseIssue = "001"; String policeName = "XX派出所"; String caseName = "0727電動車盜竊案"; String caseTime = "2018-07-26 12:20:22"; //值 Object[] Vals = new Object[]{caseIssue,policeName, caseName, caseTime}; //調用介面 doc.getMailMerge().execute(Flds, Vals); doc.save(destdoc); System.out.println("完成"); }
4,結果
其中設定域的圖片引用於http://www.xiaoguo123.com/p/aspose_words_java_template/
Aspose.words Java基於模板產生word之純文字內容