当前位置:   article > 正文

文件下载:POI读取word或Excel,修改内容后以流的形式输出到前端_poi文档下载时写入流

poi文档下载时写入流

以下是WORD实例,此方案适用于EXCEL,不用使用绝对路径,对于数据安全有一定保障

/**
     * @author lcx
     * @param id    
     * @param request
     * @param response
     */
    @RequestMapping(value="/printhelp")
    public void printhelp(HttpServletRequest request ,HttpServletResponse response) {
        logger.info(null);
        String path = request.getRealPath("") + File.separatorChar;
        String filepath = path.concat("static/template/checkrepositorytemplate.doc");
        File fis=new File(filepath);
        InputStream in = null;
        try {
            in = new FileInputStream(fis);  
            HWPFDocument hdt = new HWPFDocument(in);
            Range range = hdt.getRange();

            Map<String, String> params = new HashMap<String, String>();
            params.put("${checkdate}",  "你好");
            params.put("${repaddress}",  "你好");
            params.put("${checkusers}",  "你好");
            params.put("${checkscope}",  "你好");
            params.put("${defects}",  "你好");
            params.put("${faded}",  "你好");
            params.put("${damage}", "你好");
            params.put("${mildew}",  "你好");
            params.put("${motheaten}",  "你好");
            params.put("${leaks}",  "你好");
            params.put("${exstate}", "你好");
            params.put("${takesteps}", "你好");
            //params.put("${note}",  dto.get("note")==null?"":dto.get("note").toString());
            //把word中包含params key的内容转换成value的内容
            for (Map.Entry<String,String> entry:params.entrySet()) {
                range.replaceText(entry.getKey(),entry.getValue());
            }

            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + "checkrepositorytemplate.doc" + "\"");

            ByteArrayOutputStream ostream = new ByteArrayOutputStream();
            ServletOutputStream servletOS = response.getOutputStream(); //输出流
            hdt.write(ostream);//数据写入到输出流           
            servletOS.write(ostream.toByteArray());         

            servletOS.flush();
            servletOS.close();
            ostream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{   
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/842574
推荐阅读
相关标签
  

闽ICP备14008679号