写入word模板

    技术2022-07-11  90

    1.工具类

    /** * 导出word * <p>第一步生成替换后的word文件,只支持docx</p> * <p>第二步下载生成的文件</p> * <p>第三步删除生成的临时文件</p> * 模版变量中变量格式:{{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<String, Object> 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(); doc.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 导出word * <p>第一步生成替换后的word文件,只支持docx</p> * <p>第二步下载生成的文件</p> * <p>第三步删除生成的临时文件</p> * 模版变量中变量格式:{{foo}} * @param templatePath word模板地址 * @param fileName 文件名 * @param params 替换的参数 * @param request HttpServletRequest * @param response HttpServletResponse */ public static void exportWord(String templatePath,String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) { Assert.notNull(templatePath,"模板路径不能为空"); Assert.notNull(fileName,"导出文件名不能为空"); Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式"); 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 = 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(); File file = new File(tmpPath); if(file.exists()){ file.delete(); } } catch (Exception e) { e.printStackTrace(); } finally { // delAllFile(temDir);//这一步看具体需求,要不要删 } } /** * 导出word方法 * @param request * @param response * @param html * @param title */ public static void exportWords(HttpServletRequest request, HttpServletResponse response, String html, String title) { ServletOutputStream ostream = null; POIFSFileSystem poifs = null; ByteArrayInputStream bais = null; try { String content = html; //设置编码 byte b[] = content.getBytes("utf-8"); bais = new ByteArrayInputStream(b); /* 关键地方、成word格式 */ poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); request.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); response.addHeader("Content-Disposition", "attachment;filename=" + new String(title.getBytes("GB2312"), "iso8859-1") + ".doc"); ostream = response.getOutputStream(); poifs.writeFilesystem(ostream); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bais != null) { bais.close(); } if (ostream != null) { ostream.close(); } if (poifs != null) { poifs.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } /** * 导出word * <p>第一步生成替换后的word文件,只支持docx</p> * <p>第二步下载生成的文件</p> * <p>第三步删除生成的临时文件</p> * 模版变量中变量格式:{{foo}} * @param templatePath word模板地址 * @param temDir 生成临时文件存放地址 * @param fileName 文件名 * @param params 替换的参数 * @param request HttpServletRequest * @param response HttpServletResponse */ public static File exportWordNotDownLoad(String templatePath, String temDir, String fileName, Map<String, Object> 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(); } FileOutputStream fos = null; try { XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params); String tmpPath = temDir + fileName; fos = new FileOutputStream(tmpPath); doc.write(fos); fos.close(); File file = new File(tmpPath); return file; } catch (Exception e) { e.printStackTrace(); return null; } }

    2.将模板放入相应位置 3.参数,将写入的参数配置在文档模板中 4.引用

    exportWord("word/docwordww.docx(模板路径)", "导出名称.docx", convertBeanToMap(sysTransferDoc), request, response); /** * 将Object对象里面的属性和值转化成Map对象 * * @return * @throws IllegalAccessException */ public static Map<String, Object> convertBeanToMap(Object object) { if (object == null) { return null; } Map<String, Object> map = new HashMap<>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (!key.equals("class")) { Method getter = property.getReadMethod(); Object value = getter.invoke(object); if (Objects.isNull(value)) { value = " "; } map.put(key, value); } } } catch (Exception e) { e.printStackTrace(); } return map; }
    Processed: 0.009, SQL: 9