word上使用merge field

Merge mail是允許一個(gè)文檔模板從一個(gè)數(shù)據(jù)源中提取關(guān)鍵字段,替換模板中的內(nèi)容,生成多個(gè)文檔。特別適用于通知、群發(fā)郵件、證書(shū)的生成。


一圖了然

1. 在代碼里面,引用jar包和license.xml

<!-- word轉(zhuǎn)pdf依賴,本地jar包依賴 -->

? ? <groupId>com.aspose.word</groupId>

2. 用map對(duì)應(yīng)起來(lái)就可以進(jìn)行替換了

public MapgetMapForDoc(String userName, String userID, String applicationReason, String results){

? ? Map map =new HashMap();

? ? int number =userMapper.selectNumber()+1;

? ? map.put("number", String.valueOf(number));

? ? map.put("userName", userName);

? ? map.put("userID",userID);

? ? map.put("applicationReason",applicationReason);

? ? map.put("results",results);

? ? map.put("today",sdf.format(new Date()));

? ? return map;

}

3. 再把map中數(shù)據(jù)讀出來(lái):

private void merge(Document doc, Map data) {

if (data ==null || data.isEmpty()) {

log.info("data為空!無(wú)法繼續(xù)執(zhí)行merge!");

return;

? ? }

Set keys = data.keySet();

? ? String[] fieldNames =new String[keys.size()];

? ? Object[] values =new String[keys.size()];

? ? int i =0;

? ? for (String key : keys) {

fieldNames[i] = key;

? ? ? ? values[i] = data.get(key);

? ? ? ? ++i;

? ? }

try {

doc.getMailMerge().execute(fieldNames, values);

? ? }catch (Exception e) {

e.printStackTrace();

? ? }

}

4. 最后merge(doc, data);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容