南宁网站seo排名优化,达内网站开发课程,陕西省建设厅八大员证,番禺网站建设三杰科技今天跟大家分享一下工作中比较实用的导出word 带图片的功能。
对于在idea开发中我们需要引入以下依赖#xff1a; 2.对于eclipse 开发我们需要进入对应的jar包 这个必须放在lib下#xff0c;同样也需要在当前项目的环境是加入该依赖 需要在MEAT-INF加入 首先制定word 导出…今天跟大家分享一下工作中比较实用的导出word 带图片的功能。
对于在idea开发中我们需要引入以下依赖 2.对于eclipse 开发我们需要进入对应的jar包 这个必须放在lib下同样也需要在当前项目的环境是加入该依赖 需要在MEAT-INF加入 首先制定word 导出模版格式用占位符进行代替 对于插入的图片提前设置好大小格式
编写好后进行另存为 html 格式或者xml 接着用编辑文本打开修改里面的内容 对于插入图片的时候请注意在src 的时候也用占位符。 注意所有的占位符要与代码中的Hashmap 中的key保持一致。
编写代码 RequestMapping(value /exportWord)public void exportWord(HttpServletRequest request, HttpServletResponse response, RequestParam(value id) String id ) throws Exception{ListMapString, Object list new ArrayListMapString,Object();ArrayListString parameters new ArrayListString();ListMapString,Object queryResult2 limitspaceworkBizc.findyxkjzydc(id);if(queryResult2.size()0){list.add(queryResult2.get(0));}MapString, Object query list.get(0);exportWord(request, response, query, 明细表.docx,id);}public void exportWord(HttpServletRequest request,HttpServletResponse response, MapString, Object query, String fileName,String id) throws IOException, InvalidFormatException {//获取 模版的路径String pathString request.getSession().getServletContext().getRealPath(/WEB-INF/templete/);System.out.println(获取到的模板路径是templetePath------- pathString);Configuration configuration new Configuration(new Version(2.3.23));configuration.setDefaultEncoding(utf-8);configuration.setDirectoryForTemplateLoading(new File(pathString));Template freemarkerTemplate configuration.getTemplate(yxkjzydmysd.ftl);File file null;InputStream fin null;ServletOutputStream out null;try {String SPDimage null;ListLimitSpaceWorkExportpo vos new ArrayListLimitSpaceWorkExportpo();LimitSpaceWorkExportpo aa new LimitSpaceWorkExportpo();MapString,Object map3limitspaceworkBizc.findrwtp(id);MapString,Object map new HashMapString,Object();map.put(BH, query.get(BH)null?:query.get(BH).toString());map.put(ZCGLDW, query.get(ZCGLDW)null?:query.get(ZCGLDW).toString());map.put(SZDQ, query.get(SZDQ)null?:query.get(SZDQ).toString());map.put(ZYLX, query.get(ZYLX)null?:query.get(ZYLX).toString());map.put(ZYMC, query.get(ZYMC)null?:query.get(ZYMC).toString());map.put(GCXZ, query.get(GCXZ)null?:query.get(GCXZ).toString());map.put(GCZZDW, query.get(GCZZDW)null?:query.get(GCZZDW).toString());map.put(LXR, query.get(LXR)null?:query.get(LXR).toString());map.put(LXDH, query.get(LXDH)null?:query.get(LXDH).toString());map.put(ZYFW, query.get(ZYFW)null?:query.get(ZYFW).toString());map.put(ZYNR, query.get(ZYNR)null?:query.get(ZYNR).toString());map.put(ZYSQR, query.get(ZYSQR)null?:query.get(ZYSQR).toString());map.put(ZYDW, query.get(ZYDW)null?:query.get(ZYDW).toString());map.put(ZYSQRDH, query.get(ZYSQRDH)null?:query.get(ZYSQRDH).toString());map.put(XCFZR, query.get(XCFZR)null?:query.get(XCFZR).toString());map.put(XCFZRDH, query.get(XCFZRDH)null?:query.get(XCFZRDH).toString());map.put(AQXYS, map3.get(AQXYS)null?:map3.get(AQXYS).toString());map.put(SPD, map3.get(SPD)null?:map3.get(SPD).toString());map.put(CND, map3.get(CND)null?:map3.get(CND).toString());map.put(GZS, map3.get(GZS)null?:map3.get(GZS).toString());map.put(GJCX, map3.get(GJCX)null?:map3.get(GJCX).toString());// 调用工具类的createDoc方法生成Word文档file createDoc(map,freemarkerTemplate);fin new FileInputStream(file);response.setCharacterEncoding(utf-8);response.setContentType(application/msword);// 设置浏览器以下载的方式处理该文件名fileName URLEncoder.encode(断面验收报告 .docx, UTF-8);response.setHeader(Content-disposition, attachment; filename fileName ;filename*utf-8 fileName);out response.getOutputStream();byte[] buffer new byte[512]; // 缓冲区int bytesToRead -1;// 通过循环将读入的Word文件的内容输出到浏览器中while((bytesToRead fin.read(buffer)) ! -1) {out.write(buffer, 0, bytesToRead);}} finally {if(fin ! null) fin.close();if(out ! null) out.close();if(file ! null) file.delete(); // 删除临时文件}} 指定编码格式 private static File createDoc(MapString, Object dataMap, Template template) {String name .doc;File f new File(name);Template t template;try {// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开Writer w new OutputStreamWriter(new FileOutputStream(f), utf-8);t.process(dataMap, w);w.close();} catch (Exception ex) {ex.printStackTrace();throw new RuntimeException(ex);}return f;} 效果图展示 若本文对你有所帮助请一键三连就是对我最好的支持。