public classWordUtil {/*** 根据模板生成新word文档
* 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
*@paraminputUrl 模板存放地址
*@paramoutputUrl 新文档存放地址
*@paramtextMap 需要替换的信息集合
*@paramtableList 需要插入的表格信息集合
*@return成功返回true,失败返回false*/
public static booleanchangWord(String inputUrl, String outputUrl,
Map textMap, ListtableList) {//模板转换默认成功
boolean changeFlag = true;try{//获取docx解析对象
XWPFDocument document = newXWPFDocument(POIXMLDocument.openPackage(inputUrl));//解析替换文本段落对象
changeText(document, textMap);//解析替换表格对象
changeTable(document, textMap, tableList);//生成新的word
File file = newFile(outputUrl);
FileOutputStream stream= newFileOutputStream(file);
document.write(stream);
stream.close();
}catch(IOException e) {
e.printStackTrace();
changeFlag= false;
}returnchangeFlag;
}/*** 替换段落文本
*@paramdocument docx解析对象
*@paramtextMap 需要替换的信息集合*/
private static void changeText(XWPFDocument document, MaptextMap){//获取段落集合
List paragraphs =document.getParagraphs();for(XWPFParagraph paragraph : paragraphs) {//判断此段落时候需要进行替换
String text =paragraph.getText();if(checkText(text)){
List runs =paragraph.getRuns();for(XWPFRun run : runs) {//替换模板原来位置
run.setText(changeValue(run.toString(), textMap),0);
}
}
}
}/*** 替换表格对象方法
*@paramdocument docx解析对象
*@paramtextMap 需要替换的信息集合
*@paramtableList 需要插入的表格信息集合*/
private static void changeTable(XWPFDocument document, MaptextMap,
ListtableList){//获取表格对象集合
List tables =document.getTables();for(XWPFTable table : tables) {//只处理行数大于等于2的表格,且不循环表头
if (table.getRows().size() > 1) {//判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
if(checkText(table.getText())) {
List rows =table.getRows();//遍历表格,并替换模板
eachTable(rows, textMap);
}else{
insertTable(table, tableList);
}
}
}
}/*** 遍历表格
*@paramrows 表格行对象
*@paramtextMap 需要替换的信息集合*/
private static void eachTable(List rows ,MaptextMap){for(XWPFTableRow row : rows) {
List cells =row.getTableCells();for(XWPFTableCell cell : cells) {//判断单元格是否需要替换
if(checkText(cell.getText())){
List paragraphs =cell.getParagraphs();for(XWPFParagraph paragraph : paragraphs) {
List runs =paragraph.getRuns();for(XWPFRun run : runs) {
run.setText(changeValue(run.toString(), textMap),0);
}
}
}
}
}
}/*** 为表格插入数据,行数不够添加新行
*@paramtable 需要插入数据的表格
*@paramtableList 插入数据集合*/
private static void insertTable(XWPFTable table, ListtableList){//创建行,根据需要插入的数据添加新行,不处理表头
for(int i = 1; i < tableList.size(); i++){
XWPFTableRow row=table.createRow();
}//遍历表格插入数据
List rows =table.getRows();for(int i = 1; i < rows.size(); i++){
XWPFTableRow newRow=table.getRow(i);
List cells =newRow.getTableCells();for(int j = 0; j < cells.size(); j++){
XWPFTableCell cell=cells.get(j);
cell.setText(tableList.get(i-1)[j]);
}
}
}/*** 判断文本中时候包含$
*@paramtext 文本
*@return包含返回true,不包含返回false*/
private static booleancheckText(String text){boolean check = false;if(text.indexOf("$")!= -1){
check= true;
}returncheck;
}/*** 匹配传入信息集合与模板
*@paramvalue 模板需要替换的区域
*@paramtextMap 传入信息集合
*@return模板需要替换区域信息集合对应值*/
private static String changeValue(String value, MaptextMap){
Set> textSets =textMap.entrySet();for (Map.EntrytextSet : textSets) {//匹配模板与替换值 格式${key}
String key = "${"+textSet.getKey()+"}";if(value.indexOf(key)!= -1){
value=textSet.getValue();
}
}//模板未匹配到区域替换为空
if(checkText(value)){
value= "";
}returnvalue;
}
}