欢迎来到天天文库
浏览记录
ID:59253134
大小:1.08 MB
页数:3页
时间:2020-09-08
《POI调用Word动态传参.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、POI调用Word动态传参1功能描述实现动态传参给Word模板,然后替换模板中的参数,最后生成目的文档;传入参数分别是源文件路径,目的文件路径和匹配MAP2实现所需JAR包poi-scratchpad-3.8-.jar(文档最后有提供)3代码实现://替换word中需要替换的特殊字符publicstaticbooleanreplaceWord(StringsrcPath,StringdestPath,Mapmap){String[]sp=srcPath.split("\.");String[]dp=destPath.split("\.");if((s
2、p.length>0)&&(dp.length>0)){//判断文件有无扩展名//比较文件扩展名if(sp[sp.length-1].equalsIgnoreCase("docx")){try{XWPFDocumentdocument=newXWPFDocument(POIXMLDocument.openPackage(srcPath));//替换段落中的指定文字IteratoritPara=document.getParagraphsIterator();while(itPara.hasNext()){XWPFParagraphparagraph=(X
3、WPFParagraph)itPara.next();Stringstr=paragraph.getText();for(Map.Entryentry:map.entrySet()){str=str.replace(entry.getKey(),entry.getValue());}Listruns=paragraph.getRuns();for(inti=0;i4、=0){runs.get(i).setText(str,0);//将第一个XWPFRun的值存整个XWPFParagraph的字符串}}}//替换表格中的指定文字IteratoritTable=document.getTablesIterator();while(itTable.hasNext()){XWPFTabletable=(XWPFTable)itTable.next();intrcount=table.getNumberOfRows();for(inti=0;i5、istcells=row.getTableCells();for(XWPFTableCellcell:cells){StringcellTextString=cell.getText();for(Entrye:map.entrySet()){if(cellTextString.contains(e.getKey()))cellTextString=cellTextString.replace(e.getKey(),e.getValue());}cell.removeParagraph(0);cell.setText(cell6、TextString);}}}FileOutputStreamoutStream=null;outStream=newFileOutputStream(destPath);document.write(outStream);outStream.close();returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}else{returnfalse;}}else{returnfalse;}}调用参考如下:StringfilepathString="D:/2.docx";StringdestpathString="D7、:/2ttt.docx";Mapmap=newHashMap();map.put("${BANKNAME}","中信");map.put("${BANK}","中信银行");map.put("${TRANNAME}","亿量");map.put("${LAWYER}","周攀");map.put("${YEAR}","2015");map.put("${MONTH}","09");map.put(
4、=0){runs.get(i).setText(str,0);//将第一个XWPFRun的值存整个XWPFParagraph的字符串}}}//替换表格中的指定文字IteratoritTable=document.getTablesIterator();while(itTable.hasNext()){XWPFTabletable=(XWPFTable)itTable.next();intrcount=table.getNumberOfRows();for(inti=0;i5、istcells=row.getTableCells();for(XWPFTableCellcell:cells){StringcellTextString=cell.getText();for(Entrye:map.entrySet()){if(cellTextString.contains(e.getKey()))cellTextString=cellTextString.replace(e.getKey(),e.getValue());}cell.removeParagraph(0);cell.setText(cell6、TextString);}}}FileOutputStreamoutStream=null;outStream=newFileOutputStream(destPath);document.write(outStream);outStream.close();returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}else{returnfalse;}}else{returnfalse;}}调用参考如下:StringfilepathString="D:/2.docx";StringdestpathString="D7、:/2ttt.docx";Mapmap=newHashMap();map.put("${BANKNAME}","中信");map.put("${BANK}","中信银行");map.put("${TRANNAME}","亿量");map.put("${LAWYER}","周攀");map.put("${YEAR}","2015");map.put("${MONTH}","09");map.put(
5、istcells=row.getTableCells();for(XWPFTableCellcell:cells){StringcellTextString=cell.getText();for(Entrye:map.entrySet()){if(cellTextString.contains(e.getKey()))cellTextString=cellTextString.replace(e.getKey(),e.getValue());}cell.removeParagraph(0);cell.setText(cell
6、TextString);}}}FileOutputStreamoutStream=null;outStream=newFileOutputStream(destPath);document.write(outStream);outStream.close();returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}else{returnfalse;}}else{returnfalse;}}调用参考如下:StringfilepathString="D:/2.docx";StringdestpathString="D
7、:/2ttt.docx";Mapmap=newHashMap();map.put("${BANKNAME}","中信");map.put("${BANK}","中信银行");map.put("${TRANNAME}","亿量");map.put("${LAWYER}","周攀");map.put("${YEAR}","2015");map.put("${MONTH}","09");map.put(
此文档下载收益归作者所有