springboot利用freemark模板导出word,并且一个word多页

1.具体达到什么效果

如下

在这里插入图片描述

我想导出第一条记录,导出的word,如下

在这里插入图片描述

即,将数据放到word里

2.怎么实现

2.1创建一个word,如上,将假数据放到word里(注意,如果是导出多条数据,即一个word,多页,每一页代表一条记录,word的最下面的一行一定要写点东西,然后设置为透明,否则多条数据就会挤在一起,而不是一条记录占据一页),如下

在这里插入图片描述

2.2将word另存为.xml

2.3利用nodpad++或者editplus等打开.xml文件,将假数据换成你对应数据库的的字段,如下

在这里插入图片描述

上面本来是${usedate!},用的是ftl语法,但导出列表的话,即一个word多页,每一页代表一条记录,我用到了循环,如下

在这里插入图片描述

循环结尾如下

在这里插入图片描述

位置不要放错,</w:body>标签上下

2.3将.xml文件另存为.ftl,(2.2和2.3顺序无所谓)

2.4 将.ftl放到项目里,下面是目录结构

在这里插入图片描述

2.5 application.properties

spring.freemarker.template-loader-path=/WEB-INF/static/ftl/

2.6 controller

 @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;
    
      @RequestMapping(value = "toword", method = RequestMethod.GET)
    public void downloadWord(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "seqs") String seqs) throws Exception {// seqs为多条记录的id,用逗号隔开的字符串
        try {
            Map<String, Object> dataMap = new HashMap<>();
            String[] arrseq = seqs.split(",");
            List<LegLetter> list=iLegLetterService.getListBySeqs(arrseq);
            for(LegLetter e:list){
                e.setColumn1(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                e.setColumn2(String.valueOf(e.getAmt()));// 逗号的问题
            }
            //上面可以忽略,只是获取数据列表
            dataMap.put("leglist",list);
            //创建配置实例对象
            Configuration configuration= new Configuration(
                    Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
            //设置编码
            configuration.setDefaultEncoding("UTF-8");
            //获取ftl模板对象
//            Template template = configuration.getTemplate("leg.ftl");
            //输出文档
            String  fileName = "test数据模板.docx";
            try {
                Template template = freeMarkerConfigurer.getConfiguration().getTemplate("leg_new.ftl");
                response.setContentType("application/octet-stream");
                response.setHeader("content-Type", "application/msword");
                response.setHeader("Content-Disposition", "attachment;filename="
                        + new String(fileName.getBytes("GBK"), "ISO-8859-1"));
                response.setCharacterEncoding("utf-8");//处理乱码问题
                //生成Word文档
                template.process(dataMap, response.getWriter());
                //批量更新状态为已打印
                HashMap<String, Object> hashMap = new HashMap();
                hashMap.put("seqs", arrseq);
                hashMap.put("status", "2");
                iLegLetterService.updateBySeqs(hashMap);
            } catch (Exception e) {
                logger.info(new Date()+"----------异常信息info------------------"+e.getMessage()+"------------------------");
                logger.error(new Date()+"----------异常信息error------------------"+e.getMessage()+"------------------------");
                e.printStackTrace();
            } finally {
                response.flushBuffer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.上面的service可以忽略不记,用自己的就行

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值