poi操作word模板替换数据并且导出word | 您所在的位置:网站首页 › poi读取word › poi操作word模板替换数据并且导出word |
一、前言 poi操作word模板替换数据,很简单,涉及的算法也不是很多。我会提供poi的jar包。核心思想就是将word需要替换的部分写成关键字,在操作word的时候,根据关键字替换成我们填充的数据。 二、poi下载 微信公众号搜索:小白XBIT 回复关键字:poi,既可以下载。 三、实现 1、将word模板放在固定的路径如:D:\poi\inpoi\征信\****.docx 2、将你想要替换的数据和关键字匹配进行替换。通过java的Map构造类似json的数据格式,将获取的数据put进去。这里需要注意,map的key一定需要和word 的模板key相同,才可以替换。还需要的注意的是,如果替换不成功,需要将word的key在写字本重新写一下,复制粘贴到word就可以了。 3、核心代码 (1)将前端发过来的数据,放入map中 关键构造map进行数据替换,一会儿的工具类需要用到map /* * 处理借款人及保证人的身份证信息 */ @RequestMapping("jkr") @ResponseBody public JSONObject test(@RequestParam("name") String name,@RequestParam("sex") String sex,@RequestParam("idnum") String idnum,@RequestParam("add") String add,@RequestParam("tel") String tel,@RequestParam("type") String type, HttpServletRequest request) { /* * 借款人===1 map1 * 借款人配偶===2 map2 * 保证人1===3 map3 * 保证人1配偶===4 map4 * 保证人2===5 map5 * 保证人2配偶===6 map6 * 保证人3===7 map7 * 保证人3配偶===8 map8 */ if(type.equals("借款人")) { request.getSession().setAttribute("type1", type); //新建map1 Map map1=new HashMap(); map1.put("name1", name); map1.put("sex1", sex); map1.put("idnum1", idnum); map1.put("add1", add); map1.put("tel1", tel); //将借款人的信息放入session中 request.getSession().setAttribute("jkr", map1); //System.out.println(type+map1); }(2)进行替换 //文件路径 String srcPath = "D:\\poi\\inpoi\\征信\\****.docx"; //新文件路径 String destPath = "D:\\poi\\outpoi\\****.doc"; wordUtils word=new wordUtils(); //完后word转换 String status=word.writer(srcPath, destPath, combineResultMap);wordUtils是实现word文本替换的工具类 具体代码如下 public class wordUtils { public String allwrite(String inputSrc, String outSrc, Map map) { try { //判断文件是否存在 File file = new File(inputSrc); if(!file.exists()){ return "fail"; }else { //得到.docx文件提取器 XWPFDocument doc = new XWPFDocument(POIXMLDocument.openPackage(inputSrc)); /** * 替换段落中指定的文本 */ for(XWPFParagraph p : doc.getParagraphs()){ List runs = p.getRuns(); if(runs != null){ for(XWPFRun r : runs){ //需要替换的文本 String text = r.getText(0); //替换指定的文本 for(String key : map.keySet()){ if(text != null && text.equals(key)){ //替换的时候要注意,setText是有两个参数的 //第一个是替换的文本,第二个是从哪里开始替换 //0是替换全部,如果不设置那么默认就是从原文字 //结尾开始追加 r.setText(map.get(key),0); } } } } } for(XWPFTable tab : doc.getTables()){ for(XWPFTableRow row : tab.getRows()){ for(XWPFTableCell cell : row.getTableCells()){ //注意,getParagraphs一定不能漏掉 //因为一个表格里面可能会有多个需要替换的文字 //如果没有这个步骤那么文字会替换不了 for(XWPFParagraph p : cell.getParagraphs()){ for(XWPFRun r : p.getRuns()){ String text = r.getText(0); for(String key : map.keySet()){ if(text.equals(key)){ r.setText(map.get(text),0); } } } } } } } doc.write(new FileOutputStream(outSrc)); return "succ"; } } catch (IOException e) { e.printStackTrace(); return "succ"; } } /* * 处理word里边的段落 */ public String wzwrite(String inputSrc, String outSrc, Map map) { try { //判断文件是否存在 File file = new File(inputSrc); if(!file.exists()){ return "fail"; }else { //得到.docx文件提取器 XWPFDocument doc = new XWPFDocument(POIXMLDocument.openPackage(inputSrc)); /** * 替换段落中指定的文本 */ for(XWPFParagraph p : doc.getParagraphs()){ List runs = p.getRuns(); if(runs != null){ for(XWPFRun r : runs){ //需要替换的文本 String text = r.getText(0); //替换指定的文本 for(String key : map.keySet()){ if(text != null && text.equals(key)){ //替换的时候要注意,setText是有两个参数的 //第一个是替换的文本,第二个是从哪里开始替换 //0是替换全部,如果不设置那么默认就是从原文字 //结尾开始追加 r.setText(map.get(key),0); } } } } } doc.write(new FileOutputStream(outSrc)); return "succ"; } } catch (IOException e) { e.printStackTrace(); return "succ"; } } /* * 处理word里边的表格 */ public static String writer(String inputSrc, String outSrc, Map map) { try { //判断文件是否存在 File file = new File(inputSrc); if(!file.exists()){ return "fail"; }else { //得到.docx文件提取器 XWPFDocument doc = new XWPFDocument(POIXMLDocument.openPackage(inputSrc)); System.out.println("类型:"+doc.getClass().getName().toString()); /** * 获取所有段落:List paragraphs = word.getParagraphs(); 获取一个段落中的所有Runs:List xwpfRuns = xwpfParagraph.getRuns(); 获取一个Runs中的一个Run:XWPFRun run = xwpfRuns.get(index); doc.getTables() 获取所有表格 tab.getRows() 获取一个表格中的所有行 row.getTableCells() 获取一行中的所有列 cell.getParagraphs() 获取一格里的内容 */ for(XWPFTable tab : doc.getTables()){ for(XWPFTableRow row : tab.getRows()){ for(XWPFTableCell cell : row.getTableCells()){ //注意,getParagraphs一定不能漏掉 //因为一个表格里面可能会有多个需要替换的文字 //如果没有这个步骤那么文字会替换不了 for(XWPFParagraph p : cell.getParagraphs()){ for(XWPFRun r : p.getRuns()){ String text = r.getText(0); for(String key : map.keySet()){ if(text.equals(key)){ r.setText(map.get(text),0); } } } } } } } doc.write(new FileOutputStream(outSrc)); return "succ"; } } catch (IOException e) { e.printStackTrace(); return "succ"; } } }由于涉及工作,不方便将word 的模板给出来,答题实现思路就是这样。 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |