jeecgBoot导出Word

1、在jeecg-boot-parent的pom.xml中添加以下依赖:

    cn.afterturn

    easypoi-base

    3.0.3

    cn.afterturn

    easypoi-web

    3.0.3

    cn.afterturn

    easypoi-annotation

    3.0.3

2、添加导出Word的方法:

/**

* 导出word

*

第一步生成替换后的word文件,只支持docx

*

第二步下载生成的文件

*

第三步删除生成的临时文件

* 模版变量中变量格式:{{foo}}

*

* @param templatePath word模板地址

* @param temDir      生成临时文件存放地址

* @param fileName    文件名

* @param params      替换的参数

* @param request      HttpServletRequest

* @param response    HttpServletResponse

*/

public static void exportWord(String templatePath, String temDir, String fileName, Map params, HttpServletRequest request, HttpServletResponse response) {

    Assert.notNull(templatePath,"模板路径不能为空");

    Assert.notNull(temDir,"临时文件路径不能为空");

    Assert.notNull(fileName,"导出文件名不能为空");

    Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式");

    if (!temDir.endsWith("/")) {

        temDir = temDir + File.separator;

    }

    File dir =new File(temDir);

    if (!dir.exists()) {

        dir.mkdirs();

    }

    try {

        String userAgent = request.getHeader("user-agent").toLowerCase();

        if (userAgent.contains("msie") || userAgent.contains("like gecko")) {

            fileName = URLEncoder.encode(fileName,"UTF-8");

        }else {

            fileName =new String(fileName.getBytes("utf-8"),"ISO-8859-1");

        }

        XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);

        String tmpPath = temDir + fileName;

        FileOutputStream fos =new FileOutputStream(tmpPath);

        doc.write(fos);

        response.setContentType("application/force-download");

        response.addHeader("Content-Disposition","attachment;fileName=" + fileName);

        OutputStream out = response.getOutputStream();

        doc.write(out);

        out.close();

    }catch (Exception e) {

        e.printStackTrace();

    }

}

3、将模板放入某一个文件夹下,模板中的变量用两个英文大括号{{}}包裹:


4、调用导出Word的方法:

public void exportSysStatistic(HttpServletRequest request, HttpServletResponse response) {

    Map params =new HashMap<>();

    params.put("year", year);

    params.put("month", month);

    params.put("day", day);

    exportWord("static/tmp/statisticWord.docx","D:/export","word.docx", params, request, response);

}

5、在ShiroConfig.java中放开访问控制:

filterChainDefinitionMap.put("/analyze/**","anon");

6、调试,浏览器中直接输入接口地址(如 http://localhost:8080/jeecg-boot/analyze/exportSysStatistic)测试。

7、如果第6步自动下载不成功,则可能为模板生成后乱码(检查编译后在target目录下的子目录中生成的模板文件是否能正常打开),需要在配置文件中进行如下配置:


标红部分为新增的配置项

    

   

        org.apache.maven.plugins

        maven-resources-plugin

        

            

                xls

                woff

                woff2

                eot

                ttf

                svg

                docx

            

        

    src/main/resources

    false

    

        **/**/*.docx

    

8、重新进行第6步操作,成功。

参考改编自:https://blog.csdn.net/weixin_34310785/article/details/89650505

你可能感兴趣的:(jeecgBoot导出Word)