DocUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.lightinit.hsdataplatformresdir.common;
  2. import freemarker.template.*;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.io.*;
  5. import java.util.Map;
  6. /**
  7. * @Author: sunxiang
  8. * @Date: 2019/3/13 15:12
  9. * @Description: 导出word工具类
  10. **/
  11. public class DocUtil {
  12. public Configuration configure=null;
  13. public DocUtil(){
  14. Version version = Configuration.getVersion();
  15. configure= new Configuration();
  16. configure.setIncompatibleImprovements(version);
  17. configure.setDefaultEncoding("utf-8");
  18. }
  19. /**
  20. * 根据Doc模板生成word文件
  21. * @param dataMap 需要填入模板的数据
  22. * @param downloadType 文件名称
  23. * @param modelPath 保存路径
  24. */
  25. public File createDoc(Map<String,Object> dataMap,String modelPath,String downloadType,HttpServletRequest request){
  26. String name = "temp" + (int) (Math.random() * 100000) + ".xls";
  27. File f = new File(name);
  28. //加载需要装填的模板
  29. Template template=null;
  30. try {
  31. //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
  32. //加载模板文件,放在/uploadFiles/file/demoDoc下
  33. System.out.println("dd");
  34. System.out.println(request.getServletPath());
  35. System.out.println(request.getContextPath());
  36. System.out.println(request.getServletContext().getRealPath("1"));
  37. configure.setServletContextForTemplateLoading(request.getServletContext(), modelPath);
  38. //设置对象包装器
  39. // configure.setObjectWrapper(new DefaultObjectWrapper());
  40. //设置异常处理器
  41. configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
  42. //定义Template对象,注意模板类型名字与downloadType要一致
  43. template=configure.getTemplate(downloadType);
  44. Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
  45. template.process(dataMap, out);
  46. out.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. } catch (TemplateException e) {
  50. e.printStackTrace();
  51. }
  52. return f;
  53. }
  54. /**
  55. * 根据Doc模板生成word文件
  56. * @param dataMap 需要填入模板的数据
  57. * @param downloadType 文件名称
  58. * @param savePath 保存路径
  59. */
  60. public void createXls(Map<String,Object> dataMap,String downloadType,String webPath,String fileName,String savePath){
  61. System.out.println(savePath.substring(savePath.length()-1));
  62. if(savePath.substring(savePath.length()-1).equals(File.separator)) {
  63. savePath = savePath + "uploadFiles" + File.separator + "file" + File.separator + "jdhDailySheet"+ File.separator;
  64. }else {
  65. savePath = savePath + File.separator + "uploadFiles" + File.separator + "file" + File.separator + "jdhDailySheet"+ File.separator;
  66. }
  67. File f = new File(savePath+fileName);
  68. //加载需要装填的模板
  69. Template template=null;
  70. try {
  71. if(!f.getParentFile().exists()){
  72. f.getParentFile().mkdirs();
  73. }
  74. if(f.exists() && f.isFile()){
  75. f.delete();
  76. } else {
  77. f.createNewFile();
  78. }
  79. //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
  80. //加载模板文件,放在/uploadFiles/file/demoDoc下
  81. configure.setDirectoryForTemplateLoading(new File(webPath + "uploadFiles" + File.separator + "file" + File.separator));
  82. //设置对象包装器
  83. // configure.setObjectWrapper(new DefaultObjectWrapper());
  84. //设置异常处理器
  85. configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
  86. //定义Template对象,注意模板类型名字与downloadType要一致
  87. template=configure.getTemplate(downloadType);
  88. Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
  89. template.process(dataMap, out);
  90. out.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. } catch (TemplateException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }