1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
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 temdir = new File(templatePath); if (!temdir.exists()) { System.out.println(temdir); } 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.setHeader("Content-Disposition", "attachment; filename=" + "strDate" + ".docx");
response.setContentType("application/force-download");
OutputStream out = response.getOutputStream(); doc.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } finally {
}
}
|