使用freemarker模板引擎导出word或excel文件(工具类)

不凡 1509 0

  此工具类是利用freemarker模板引擎 xml文件/ftl文件  成的word或excel文件

   使用freemarker模板引擎导出word或excel文件(工具类)-第1张图片-爱制作博客

package com.izhizuo.cloud.util;

import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

/**
 *
 * @author ihizuo
 * @date 2017/11/29
 * 文件导出相关的工具类
 */
public class FileExportUtil {

   /**
    * 导出word文件
    * @param templatePath
    * @param dataMap
    * @return
    * @throws IOException
    */
   public static File createFile(String templatePath, Map dataMap) throws IOException {
      Configuration configuration = new Configuration();
      configuration.setDefaultEncoding("utf-8");
      //String a = ClassUtils.getDefaultClassLoader().getResource("").getPath().replace("file:", "");
      // 模板文件全路径名切割
      int lastIndexOf = templatePath.lastIndexOf("/");
      //模板文件路径名
      String path = templatePath.substring(0, lastIndexOf);
      //模板文件名
      String name = templatePath.substring(lastIndexOf + 1);
      configuration.setClassForTemplateLoading(FileExportUtil.class, path);
      File file = new File(name);
      try {
         //configuration.setDirectoryForTemplateLoading(filePath);
         Template template = configuration.getTemplate(name);
         // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
         Writer w = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
         template.process(dataMap, w);
         w.close();
      } catch (Exception e) {
         e.printStackTrace();
         throw new IOException("创建文档出错");
      }
      return file;
   }

   /**
    * 客户端下载word文件
    * @param response
    * @param file
    * @param fileName
    * @param fileType 文件类型
    * @throws IOException
    */
   public static void downWord( HttpServletResponse response, File file, String fileName, String fileType) throws IOException {
      // 设置浏览器以下载的方式处理该文件的文件名,支持中文
      setFileDownloadHeader(response, fileName, "." + fileType);
      InputStream fin = null;
      ServletOutputStream out = null;
      try {
         // 调用工具类WordGenerator的createDoc方法生成Word文档
         fin = new FileInputStream(file);
         out = response.getOutputStream();
         byte[] buffer = new byte[1024];
         int bytesToRead = -1;
         // 通过循环将读入的Word文件的内容输出到浏览器中
         while((bytesToRead = fin.read(buffer)) != -1) {
            out.write(buffer, 0, bytesToRead);
         }
      } catch (Exception e) {
         e.printStackTrace();
         throw new IOException("下载文件出错");
      } finally {
         if(fin != null) {
            fin.close();
         }
         if(out != null) {
            out.close();
         }
         if(file != null) {
            // 删除临时文件
            file.delete();
         }
      }
   }
   /**
    * 设置让浏览器弹出下载对话框的Header
    * @param response
    * @param fileName
    * @param fileType
    */
   public static void setFileDownloadHeader(HttpServletResponse response, String fileName, String fileType){
      try {
         // 中文文件名支持
         String encodedfileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
         response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + fileType + "\"");
         response.setCharacterEncoding("utf-8");
         if (".doc".equals(fileType)){
            response.setContentType("application/msword");
         }else {
            response.setContentType("application/msexcel");
         }
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      }
   }
}

使用freemarker模板引擎导出word或excel文件(工具类)-第2张图片-爱制作博客FileExportUtil.java


标签: java 工具类 导入导出 Excel freemarker Word

发布评论 0条评论)

  • Refresh code

还木有评论哦,快来抢沙发吧~