使用Thymeleaf创建word文档

第一步:创建word模板文件

word中新建word文档 设计如下

 

 

把上面的文件用word打开,然后另存为word xml文件, 

 

用notepad++打开xml文件

使用 thymeleaf语法修改文件 修改后放到templates下

第二步使用 thymeleaf 生成word 文档

  
	        String url = "D:/";
	    	Context context = new Context();
	    	context.setVariable("company", "卓越软件");  
	    	
	    	 byte[] bytes1 = FileUtil.readFileByBytes("C:\\d.gif");
	           
	            String image1 = Base64Util.encode(bytes1);
	          
	    	context.setVariable("image", image1);
	    	
	    	Map<String,String> map1 = new HashMap<String,String>();           
            map1.put("name", "张三");
            map1.put("age", "123123");
            map1.put("sex", "男");
            Map<String,String> map2 = new HashMap<String,String>();            
            map2.put("name", "李四");
            map2.put("age", "123123");
            map2.put("sex", "男");
            List<Map<String,String>> newlist = new ArrayList<>();
            newlist.add(map1);
            newlist.add(map2);
            Map<String, Object> dataMap = new HashMap<>();
           
	    	
            context.setVariable("userList", newlist);
	    	
            response.setContentType("multipart/form-data");
            //2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)
            response.setHeader("Content-Disposition", "attachment;fileName=" + "xx.doc");
            Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"),10240);
	    	
	    	TemplateUtils.process("company.html", context, out);
	    	out.close();

相关函数:

package com.ruoyi;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

public class TemplateUtils {
	
	@Autowired
	public static final TemplateEngine engine = ApplicationContextHolder.getBean(TemplateEngine.class);
	/**
	 * 生成静态文件
	 * @param freeTempName 模板名称
	 * @param context 数据内容
	 * @param outFilePath 输出路径
	 * @return
	 */
	public static boolean process(String freeTempName,Context context,String outFilePath) {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(outFilePath);
			engine.process(freeTempName, context,fileWriter);
			
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				fileWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	
	public static boolean process(String freeTempName,Context context,Writer writer) {
		
		engine.process(freeTempName, context,writer);
		
		return true;
	}
}

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        applicationContext = ctx;
    }

 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

 
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

 
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }
}

另一篇文章
Java用freemarker导出word
https://www.cnblogs.com/demodashi/p/8458097.html 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值