1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > easypoi导出word文档(文本 表格)

easypoi导出word文档(文本 表格)

时间:2022-10-22 15:03:10

相关推荐

easypoi导出word文档(文本 表格)

文件模板

依赖导入

*坑:注意依赖版本 4.3.0+才支持多图片循环导出

<!-- word导出 方式:easypoi--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.3.0</version></dependency><!--注意:word中要使用循环等标签必须单独导入以下依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.4</version></dependency>

逻辑代码

@RequestMapping(value = "/wordExportTest",method = RequestMethod.GET)public void wordExportTest(HttpServletResponse response) throws Exception {//封装数据Map<String, Object> params = new HashMap<String, Object>();params.put("title", "你好啊");params.put("body", "一夜暴富,早日脱单");List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();Map<String, Object> obj;for (int i = 0; i < 5; i++) {obj = new HashMap<String, Object>();obj.put("name", "abc-" + i);obj.put("age", i);obj.put("sex", "男-" + i);list.add(obj);}params.put("objs", list);//jar包获取不到文件路径//URLDecoder.decode() 解决获取中文名称文件路径乱码String templatePath = URLDecoder.decode(this.getClass().getClassLoader().getResource("mb/分析报告.docx").getPath(), "UTF-8");//生成word文档流XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);//设置响应体内容类型response.setContentType("application/octet-stream");//添加响应头response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("报告.docx", "UTF-8"));//暴露新添加的响应头response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//将word文档流输出到输出流中doc.write(response.getOutputStream());//关闭流doc.close();}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。