Freemarker使用xml生成word模板

问:首先需求是什么?
答:需要生成一个word文档,其中格式内容有固定模板文件参考
首先word文档中需要动态生成的文字字段通通换成${name}的格式,其中name对应 java中的字段,图片等本篇不涉及哈,百度一下也很多就是格式不同
接着另存为.xml格式
最后文档格式改为.ftl
至此模板文件已经构建完成。

	 /**
     * 获取生成Word文档所需要的数据
     */
	@PostMapping("/getWordData")
	@ResponseBody
	public CommonResult getWordDataT(ModelMap model,Long id,HttpServletResponse response) {
		Map<String,Object> dataMap = new HashMap<String, Object>();
		//
		Archive archive=archiveService.get(id).get();
        try {
            //编号
            dataMap.put("title", archive.getTitle());
            //日期
            dataMap.put("date", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("2018-09-19")));
            //附件张数
            dataMap.put("author", archive.getAuthor());
            //受款人
            dataMap.put("payee", "张三");
            //付款用途
            dataMap.put("use_of_payment", "test");
            //小写金额
            dataMap.put("lowercase_amount", "100");
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");
 
            /**
             * 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
             * 指定ftl文件所在目录的路径,而不是ftl文件的路径
             */
            //指定路径的第一种方式(根据某个类的相对路径指定)
//                configuration.setClassForTemplateLoading(this.getClass(), "");
 
            //指定路径的第二种方式,我的路径是C:/a.ftl E:/word
            configuration.setDirectoryForTemplateLoading(new File("E:/word/"));
 
            //输出文档路径及名称
            File outFile = new File("E:/word/"+archive.getTitle()+"申报单.doc");
 
            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate("23.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new 				   FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
        //这边是生成word 文档后直接找开
			Runtime.getRuntime().exec("cmd /c start "+"E:/word/"+archive.getTitle()+"申报单.doc");
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
    	return new CommonResult().Success();

	}

其中.tfl文档生成时会产生一个问题,基本上都是因为复制${}表达式的问题,建议先测试一个字段是否能正确生成,所有的 ${}都手动输入比较好。

     freemarker.core.ParseException: Encountered "<" at line 3, column 28888 in test.ftl.  
    Was expecting one of:  
        <STRING_LITERAL> ...  
        <RAW_STRING> ...  
        "false" ...  
        "true" ...  
        <INTEGER> ...  
        <DECIMAL> ...  
        "." ...  
        "+" ...  
        "-" ...  
        "!" ...  
        "[" ...  
        "(" ...  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值