freemarker导出wordwen‘dan

废话不说,上代码!

一、导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <version>2.5.2</version>
</dependency>

二、从配置文件中读取word模板路径以及文件生成的父目录

package com.shangyuninfo.core.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


@Component
@Configuration
public class WordCreateConfig {

	private static String templateBaseDir;

	private static  String fileBaseDir;

	public static String getTemplateBaseDir() {
		return templateBaseDir;
	}

	@Value("${word.templateBaseDir}")
	public void setTemplateBaseDir(String templateBaseDir) {
		WordCreateConfig.templateBaseDir = templateBaseDir;
	}

	public static String getFileBaseDir() {
		return fileBaseDir;
	}

	@Value("${word.fileBaseDir}")
	public void setFileBaseDir(String fileBaseDir) {
		WordCreateConfig.fileBaseDir = fileBaseDir;
	}
}

三、根据xml模板生成doc文件

package com.shangyuninfo.core.util;

import cn.hutool.core.util.StrUtil;
import com.shangyuninfo.core.configuration.WordCreateConfig;
import com.shangyuninfo.core.exception.BusinessException;

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

import java.io.*;
import java.util.Map;


public class WordUtil {

	/**
     * @Title: createWord
     * @Description: TODO  将数据添加到xml中,并生成word文件
     * @param: map  需要替换数据的map
     * @param: templateName  模板名称
     * @param filePath      想要生成的文件的路径
     * @param fileName      想要生成的文件的名字
     * @return: String    返回生成文件地址
     * @throws
     */
    public static String createWord(Map map, String templateName, String filePath, String fileName) throws IOException, TemplateException {
		if (StrUtil.isBlank(WordCreateConfig.getTemplateBaseDir()) || StrUtil.isBlank(WordCreateConfig.getFileBaseDir())){
			throw new BusinessException("缺少必要参数,请在配置文件中进行配置!");
		}
    	if (StrUtil.isBlank(templateName)){
			throw new BusinessException("模板名称不能为空!");
		}
		if (StrUtil.isBlank(filePath)){
			throw new BusinessException("文件保存路径不能为空!");
		}
		if (StrUtil.isBlank(fileName)){
			throw new BusinessException("文件名不能为空!");
		}
		if (!fileName.endsWith(".doc") && !fileName.endsWith(".docx") && !fileName.endsWith(".DOC") && !fileName.endsWith(".DOC")){
			throw new BusinessException("文件扩展名错误!");
		}
		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("UTF-8");
		//word模板路径
		configuration.setDirectoryForTemplateLoading(new File(WordCreateConfig.getTemplateBaseDir()));
		Template template = configuration.getTemplate(templateName);
		//文件全路径
		String fileFullName ;
		if (filePath.startsWith("/") || filePath.startsWith("\\")){
			fileFullName = WordCreateConfig.getFileBaseDir() + filePath + File.separator + fileName;
		}else {
			fileFullName = WordCreateConfig.getFileBaseDir() + File.separator + filePath + File.separator + fileName;
		}
		File outFile = new File(fileFullName);
		if (!outFile.getParentFile().exists()) {
			outFile.getParentFile().mkdirs();
		}
		Writer out = new BufferedWriter(
				new OutputStreamWriter(
						new FileOutputStream(outFile), "UTF-8"));
		template.process(map, out);
		out.flush();
		out.close();
		return fileFullName;
    }

}

四、顺便补上模板取值

1.基本数据类型或其包装类型取值

${var}

2.对象取值

${var.property}

3.list循环

<#list dataList as data>
    ${data}
</#list>

4.map取值

<#assign map=data/>
<#assign  keys=map?keys/><!-- map的key的集合,可以配合list标签循环取值,然后根据key取value -->
<!-- 取第一个key的值 -->
${map[keys[0]]!''}

5.标记为纯文本,var是啥就是啥

<![CDATA[${var}]]>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值