用Freemarker导出编排好格式的word文档

效果图:
在这里插入图片描述
实现步骤:
1:将已编排好格式的word文档另存为xml.
2:用nodpad++等工具的xml插件格式化xml文档。
3:编写freemark标签:
在这里插入图片描述
4:响应文件流

TemplateParam genData = new TemplateParam(temlateName);
		genData.setDataMap(dataMap);//数据map
		genData.setGenFileName("xxxxx.doc");//文件名
		FreemarkerUtils.generateFile(genData, 	SystemUtils.getResponse());

FreemarkerUtils类:

public class FreemarkerUtils {

	public static void main(String[] args) {
		// 生成模板,resource/templates包
		TemplateParam templateParam = new TemplateParam("testgen.txt");
		// 生成的文件名
		templateParam.setGenFileName("测试freemarker文件生成.txt");
		// 生成的本地路径,response写出方式可不填
		templateParam.setGenFilePath("D:\\");
		// 生成的参数,也可以templateParam.setDataMap(dataMap);
		templateParam.append("param1", "这是参数1").append("param2", "这是参数2");
		// 生成文件
		generateFile(templateParam);
	}

	private static String encodeing = "utf-8";

	public static void generateFile(TemplateParam genData) {
		// 获取输出
		try (Writer out = getFileWriter(genData.getGenFilePath(), genData.getGenFileName())) {
			// 获取模板
			Template template = getFremarkerTemplate(genData.getTemplateName());
			// 写出文件
			template.process(genData.getDataMap(), out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void generateFile(TemplateParam genData, HttpServletResponse response) {
		// 获取输出
		try (Writer out = getResponseWriter(response, genData.getGenFileName())) {
			// 获取模板
			Template template = getFremarkerTemplate(genData.getTemplateName());
			// 写出文件
			template.process(genData.getDataMap(), out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static Template getFremarkerTemplate(String templateName) throws IOException {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
		cfg.setTemplateLoader(new ClassTemplateLoader(ClassUtils.getDefaultClassLoader(), "templates"));
		Template template = cfg.getTemplate(templateName, encodeing);
		template.setOutputEncoding(encodeing);
		return template;
	}

	private static OutputStreamWriter getResponseWriter(HttpServletResponse response, String respnseFileName) {
		try {
			OutputStream outputStream = response.getOutputStream();
			OutputStreamWriter out = new OutputStreamWriter(outputStream, encodeing);
			response.addHeader("Content-type", "application");
			response.setContentType("application/ms-html");
			response.setCharacterEncoding(encodeing);
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(respnseFileName.getBytes("GB2312"), "ISO_8859_1"));
			response.flushBuffer();
			return out;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	private static Writer getFileWriter(String genFilePath, String genFileName) throws IOException {
		// 创建根目录
		File file = new File(genFilePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		// 输出文档
		File outFile = new File(genFilePath + File.separator + genFileName);
		// 清掉老文件
		if (outFile.exists()) {
			outFile.delete();
		}
		return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), encodeing));
	}

	public static class TemplateParam {
		private String genFilePath;
		private String genFileName;
		private String templateName;
		private Context dataMap = new Context();
		
		public TemplateParam(String templateName) {
			this.templateName = templateName;
		}

		public String getGenFilePath() {
			if (DrinStringUtils.isNull(genFilePath)) {
				throw new LocalException("生成文件路径不能为空");
			}
			return genFilePath;
		}

		public void setGenFilePath(String genFilePath) {
			this.genFilePath = genFilePath;
		}

		public String getGenFileName() {
			if (DrinStringUtils.isNull(genFileName)) {
				throw new LocalException("生成文件名不能为空");
			}
			return genFileName;
		}

		public void setGenFileName(String genFileName) {
			this.genFileName = genFileName;
		}

		public String getTemplateName() {
			if (DrinStringUtils.isNull(templateName)) {
				throw new LocalException("模板名称不能为空");
			}
			return templateName;
		}

		public void setTemplateName(String templateName) {
			this.templateName = templateName;
		}

		public Context getDataMap() {
			if (DrinStringUtils.isNull(dataMap) || dataMap.isEmpty()) {
				throw new LocalException("模板数据不能为空");
			}
			return dataMap;
		}

		public void setDataMap(Context dataMap) {
			this.dataMap = dataMap;
		}

		public TemplateParam append(String key, Object value) {
			this.dataMap.put(key, value);
			return this;
		}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值