Freemarker导出Word工具类

转载请注明:https://blog.csdn.net/qfashly/article/details/79498984
工具类

public class FreemarkerWordUtils {
    private Logger log = LoggerFactory.getLogger(FreemarkerWordUtils.class);
    private Configuration configuration;
    private static final String ENCODING = "UTF-8";
    /**word模板存放的目录*/
    private static final String MODEL_FILE = "/com/new/template";
    /**模板文件,不需要添加目录,只需文件名*/
    private String modelFileName;
    /**生成的目标文件名*/
    private String fileName;
    /**需替换的数据Map*/
    private Map dataMap;

    /**
     * 构造函数
     * @param modelFileName 模板文件名(不用带目录)
     * @param descFileName 生成word文件地址
     * @param dataMap 文件数据
     */
    public FreemarkerWordUtils(String modelFileName, String fileName, Map dataMap) {
        configuration = new Configuration();
        configuration.setDefaultEncoding(ENCODING);
        try {
            //为了使freemarker可以使用key为非字符串的map
            configuration.setSetting("object_wrapper", "freemarker.ext.beans.BeansWrapper");
        } catch (TemplateException e) {
            log.error("导出word出错:", e);
        }
        this.modelFileName = modelFileName;
        this.fileName = AppConfig.getInstance().getValue(AppKeys.WORD_FILE_DIR) + File.separator + fileName;
        this.dataMap = dataMap;
    }

    /**
     * 创建word文件
     * @return
     */
    public File createDoc() {
        if (modelFileName == null || fileName == null) {
            return null;
        }
        // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,  
        // 这里我们的模板是放在com.gjtc.template包下面  
        configuration.setClassForTemplateLoading(this.getClass(), MODEL_FILE);
        //需要装载的模板
        Template template = null;
        try {
            template = configuration.getTemplate(modelFileName);
            template.setEncoding(ENCODING);
        } catch (IOException e) {
            log.error("获取word模版出错:", e);
        }
        int endIndex = fileName.lastIndexOf(File.separator);
        String fileDir = fileName.substring(0,endIndex);
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File resultFile = new File(fileName);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resultFile), ENCODING));
        } catch (Exception e) {
            log.error("导出word出错:", e);
        }
        try {
            template.process(dataMap, out);
        } catch (Exception e) {
            log.error("导出word模版出错:", e);
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                log.error("",e);
            }
        }
        return resultFile;
    }
}

使用

public void createReport(){
    Map<String, Object> dataMap = this.getDataMap();
    String modelFileName = "/world.ftl";
    String fileName = "D:\Myworkspace\Myeclipse\数据报表。doc";
    FreemarkerWordUtils wordUtils = new FreemarkerWordUtils(modelFileName, descFileName, fileName);
    File file = wordUtils.createDoc();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值