2023年7月3日发(作者:)
jeecgBoot导出Word1、在jeecg-boot-parent的中添加以下依赖:
第⼀步⽣成替换后的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) { l(templatePath,"模板路径不能为空"); l(temDir,"临时⽂件路径不能为空"); l(fileName,"导出⽂件名不能为空"); (th(".docx"),"word导出请使⽤docx格式"); if (!th("/")) { temDir = temDir + tor; } File dir =new File(temDir); if (!()) { (); } try { String userAgent = der("user-agent").toLowerCase(); if (ns("msie") || ns("like gecko")) { fileName = (fileName,"UTF-8"); }else { fileName =new String(es("utf-8"),"ISO-8859-1"); } XWPFDocument doc = Word07(templatePath, params); String tmpPath = temDir + fileName; FileOutputStream fos =new FileOutputStream(tmpPath); (fos); tentType("application/force-download"); der("Content-Disposition","attachment;fileName=" + fileName); OutputStream out = putStream(); (out); (); }catch (Exception e) { tackTrace(); }}3、将模板放⼊某⼀个⽂件夹下,模板中的变量⽤两个英⽂⼤括号{{}}包裹:4、调⽤导出Word的⽅法:public void exportSysStatistic(HttpServletRequest request, HttpServletResponse response) { Map params =new HashMap<>(); ("year", year); ("month", month); ("day", day); exportWord("static/tmp/","D:/export","", params, request, response);}5、在中放开访问控制:("/analyze/**","anon");7、如果第6步⾃动下载不成功,则可能为模板⽣成后乱码(检查编译后在target⽬录下的⼦⽬录中⽣成的模板⽂件是否能正常打开),需要在配置⽂件中进⾏如下配置:标红部分为新增的配置项发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688383998a129925.html
评论列表(0条)