潤乾報表制作完成後,不僅能實作展現及導出等功能,還能夠根據使用者需求, 通過指定模版檔案中的書簽名稱确定插入位置,然後将報表、 圖檔、 文本内容插入到新的 Word 檔案中。
本文主要介紹潤乾報表插入 word 文檔的具體步驟。
首先介紹下功能原理:
1、 建立 word 模版,在需要插入潤乾報表的位置定義“書簽”;
2、 Api 根據 word 書簽位置,插入計算後的報表對象;
3、 輸出根據模版生成的 word 檔案。
具體實作過程及相關代碼:
1、 建立 word 模版
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBHL0FWby9mZvwVZnFWbp1zczV2YvJHctM3cv1Ce-cnW1JkbMRzaUllaoRkWsJERP1GZqpFMFdUTp50VZtmU65EMRdkWoxGRO1mREpVaOpHTslTaipWNTJmdO1GTwolMipnRYlVe1knW0xmMMZ3bENGMShUYvwlbj5yZtlmbkN3YuQnclZnbvN2Ztl2Lc9CX6MHc0RHaiojIsJye.jpg)
2、 Api 計算報表,并通過 DocxChanger 類将報表結果插入指定書簽,輸出 word 結果
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importcom.raqsoft.dm.Sequence;
importcom.raqsoft.report.model.ReportDefine;
importcom.raqsoft.report.usermodel.Context;
importcom.raqsoft.report.usermodel.Engine;
importcom.raqsoft.report.usermodel.IReport;
importcom.raqsoft.report.util.ReportUtils;
importcom.raqsoft.report.view.oxml.word.DocxChanger;
public****classInsertWord {
publicstaticvoidmain(String[] args) {
try{
// 設定報表授權檔案
File flic =newFile(“D:/test.lic”);
FileInputStream lis =newFileInputStream(flic);
Sequence.readLicense( Sequence.P_RPT, lis);
File f =newFile(“D:/test.docx”); // 模闆檔案,注意僅僅是模版,不會被修改
File of =newFile(“D:/out.docx”); // 輸出檔案:最終根據模版生成的結果 word 檔案。
if(of.exists()) {
of.delete();
}
FileOutputStream fos =newFileOutputStream(of);
DocxChanger dc =newDocxChanger(f, fos); // 執行個體化 DocxChanger
File f3 =newFile(“D:/a.rpx”);
FileInputStream fis =newFileInputStream(f3);
IReport report = ReportUtils.read(fis);
fis.close();
// 建構報表運作上下文環境及引擎,并計算報表
Context context =newContext();
Engine engine =newEngine((ReportDefine) report, context);
report = engine.calc();
// 将報表結果指定插入 report1 書簽處,注意名字和 word 模版内書簽名一緻
dc.insertReport(“report1”, report);
// 執行所有修改動作,然後關閉輸出檔案流
dc.execute();
fos.close();
}catch(Throwable x) {
x.printStackTrace();
}
}
}
3、 對比驗證結果
(1)Word 模版(執行完 api 後,沒有改變):
(2)報表實際運作結果
(3)輸出的 word 結果檔案(根據上面代碼為 out.docx)
詳情連結:http://c.raqsoft.com.cn/article/1534476441279?r=gxy