poi读取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<String, String> map1=new HashMap<String, String>();

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<String,String> 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<XWPFRun> 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<String,String> 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<XWPFRun> 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<String,String> 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<XWPFParagraph> paragraphs = word.getParagraphs();

获取一个段落中的所有Runs:List<XWPFRun> 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 的模板给出来,答题实现思路就是这样。

实现效果在公众号也可以看到:

微信公众号搜索:小白XBIT,回复关键字poi打印可以看到打印效果

打印word文章可以看这篇:https://zhuanlan.zhihu.com/p/165458446

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容