| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package com.lightinit.hsdataplatformresdir.common;
- import freemarker.template.*;
- import javax.servlet.http.HttpServletRequest;
- import java.io.*;
- import java.util.Map;
- /**
- * @Author: sunxiang
- * @Date: 2019/3/13 15:12
- * @Description: 导出word工具类
- **/
- public class DocUtil {
- public Configuration configure=null;
- public DocUtil(){
- Version version = Configuration.getVersion();
- configure= new Configuration();
- configure.setIncompatibleImprovements(version);
- configure.setDefaultEncoding("utf-8");
- }
- /**
- * 根据Doc模板生成word文件
- * @param dataMap 需要填入模板的数据
- * @param downloadType 文件名称
- * @param modelPath 保存路径
- */
- public File createDoc(Map<String,Object> dataMap,String modelPath,String downloadType,HttpServletRequest request){
- String name = "temp" + (int) (Math.random() * 100000) + ".xls";
- File f = new File(name);
- //加载需要装填的模板
- Template template=null;
- try {
- //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
- //加载模板文件,放在/uploadFiles/file/demoDoc下
- System.out.println("dd");
- System.out.println(request.getServletPath());
- System.out.println(request.getContextPath());
- System.out.println(request.getServletContext().getRealPath("1"));
- configure.setServletContextForTemplateLoading(request.getServletContext(), modelPath);
- //设置对象包装器
- // configure.setObjectWrapper(new DefaultObjectWrapper());
- //设置异常处理器
- configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
- //定义Template对象,注意模板类型名字与downloadType要一致
- template=configure.getTemplate(downloadType);
- Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
- template.process(dataMap, out);
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TemplateException e) {
- e.printStackTrace();
- }
- return f;
- }
- /**
- * 根据Doc模板生成word文件
- * @param dataMap 需要填入模板的数据
- * @param downloadType 文件名称
- * @param savePath 保存路径
- */
- public void createXls(Map<String,Object> dataMap,String downloadType,String webPath,String fileName,String savePath){
- System.out.println(savePath.substring(savePath.length()-1));
- if(savePath.substring(savePath.length()-1).equals(File.separator)) {
- savePath = savePath + "uploadFiles" + File.separator + "file" + File.separator + "jdhDailySheet"+ File.separator;
- }else {
- savePath = savePath + File.separator + "uploadFiles" + File.separator + "file" + File.separator + "jdhDailySheet"+ File.separator;
- }
- File f = new File(savePath+fileName);
- //加载需要装填的模板
- Template template=null;
- try {
- if(!f.getParentFile().exists()){
- f.getParentFile().mkdirs();
- }
- if(f.exists() && f.isFile()){
- f.delete();
- } else {
- f.createNewFile();
- }
- //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
- //加载模板文件,放在/uploadFiles/file/demoDoc下
- configure.setDirectoryForTemplateLoading(new File(webPath + "uploadFiles" + File.separator + "file" + File.separator));
- //设置对象包装器
- // configure.setObjectWrapper(new DefaultObjectWrapper());
- //设置异常处理器
- configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
- //定义Template对象,注意模板类型名字与downloadType要一致
- template=configure.getTemplate(downloadType);
- Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
- template.process(dataMap, out);
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TemplateException e) {
- e.printStackTrace();
- }
- }
- }
|