poi實現word內容替換並且轉pdf和製作模版

2021-09-29 14:32:07 字數 3314 閱讀 4671

(請把模版製作好,尤其是下面的解決方法,一定要去修改)

(請把模版製作好,尤其是下面的解決方法,一定要去修改)

(重要的問題說三遍!!!)

製作模版

file jarf = h.getsource();

jarf.getparent()+file.separator+wordname.tostring()

先通過步驟2中的方法獲取到模版,模版可以放在資料庫,或者專案中,有檔案伺服器的就請忽略第二步

mapparams = new linkedhashmap<>();

params.put("$", dbglzbtzs.getzbxmmc());

params.put("$", dbglzbtzs.getxmfzr());

xwpfdocument document = docxutil.getdocument(filecontent);

//poi模版替換

docxutil.replaceword(document, params);

//poi實現講替換後的docx生成到某個路徑下面

docxutil.exportdocx(document,jarf.getparent()+"/",wordname.tostring());

pdfoptions options = pdfoptions.create();

//替換過資料的docx的路徑

inputstream1 = new fileinputstream(new file(wordpath1.tostring()))

; //pdf生成路徑

fileoutputstream outputstream = new fileoutputstream(new file(rootpath.tostring()))

;//word轉pdf wordtopdf.wordconvertertopdf(inputstream1,outputstream,options)

;

public class docxutil 

/** @description: 獲取word模板資料來源

* @param srcpath 路徑

* @return xwpfdocument

* @author xuweiqiang

* @date 2019/8/14

*/public static xwpfdocument getdocument(byte[

] buf) throws ioexception catch (exception e)

finally

}return null;

} /*

* @description: 匯出docx

* @param document word模板資料來源

* destpath 檔案路徑

* filename 檔名

* @return

* @author xuweiqiang

* @date 2019/8/14

*/public static byte[

] exportdocx(xwpfdocument document, string wordfilepath, string filename)

} file f = new file(wordfilepath + filename)

; out = new fileoutputstream(f)

; document.write(out)

; out.flush();

} catch (ioexception e)

finally

} catch (exception e)

}return null;

} public static byte[

] localwjtoio(string pathname)

by = bytestream.tobytearray();

} catch (ioexception ex)

finally

} catch (ioexception e)

}return by;

} /*

* @description: 替換段落中的指定文字

* @param document word模板資料來源

* @param map 關鍵字鍵值對對映

* @return

* @author xuweiqiang

* @date 2019/8/14

*/public static void replaceword(xwpfdocument document, map map)

for(map.entry entry :

map.entryset(

)) run.settext(oneparastring, 0);}

}}/*

* @description: 增加**行資料

* @param document word模板資料來源

* list 行資料

* tabnum **順序

* colnum 列數

* fontname 字型名

* fontsize 字型大小

* @return

* @author xuweiqiang

* @date 2019/8/14

*/public static void addtablerow(xwpfdocument document, list> list, int tabnum, int colnum, string fontname, int fontsize)

xwpfparagraph xp;

for(int t = 0; t < list.size(

); t++)}}

}}public static byte[

] tobytearray(inputstream input) throws ioexception

return output.tobytearray();

}}

public class wordtopdf 

/*** 替換段落中內容

*/private static void paragraphreplace(list paragraphs, map params)}}

}}}

poi操作word實現分頁

首先說下,poi實現word分頁並不像很多 說的只有一種方式!需求場景 1 在word模板基礎上多次建立乙個 模板 2 該 模板不能沾滿一頁,及時能佔滿拷貝時也存在一些問題,模板如下所示 先使用xwpfdocument.createtable 建立乙個空 xwpfdocument.settable ...

POI不同版本替換Word模板時的問題

一 問題描述 通過poi,把word中的佔位符替換為實際的值,以生成複雜結構的業務報告。在poi 3.9上,功能正常。由於某些原因公升級到poi 3.10.1後,專案組反饋說word模板出錯,無法生成word檔案,總是報解析錯誤。二 問題分析 word模板功能相關的 應該說是比較穩定了,相關 很久沒...

匯出替換Word模板中的內容

if filepath.tostring tolowercase endswith docx runtext oneparastring for map.entryentry hmocrgkeytovalue.entryset else 網上有很多先移除runs在新增,這種做法模板裡面的樣式會消失,...