springboot使用freemarker模板生成word文档

freemarker是一种web应用组件,类似thymeleaf模板引擎。

1 要利用freemarker生成word文档,首先需要创建word文档模板(即.doc文档),需要用户填写的值可以使用${string}代替,但是在后台给Model赋值的时候要名称对应,然后将后缀名改为.ftl。将如模板名称为model.ftl

2 再将model.ftl放置在springboot项目的resource目录下

3 导入freemarker依赖的jar包

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

4  配置文件

spring.freemarker.template-loader-path=/
#关闭缓存即时刷新,生产环境需要改成true;
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request

 5 页面访问控制

@RequestMapping("/tomodel.action")
    public String freemarker(Model model){
       Map<String,Object> map=new HashMap<String,Object>();
       map.put("name","张三");
       model.addAttribute("name","张三1");
    model.addAttribute("sex","男"); model.addAttribute("age","20"); model.addAttribute("xueli","本科"); model.addAttribute("live","湖南.衡阳"); model.addAttribute("hukou","湖南.郴州"); model.addAttribute("tel","15570960031");
    model.addAttribute("email","[email protected]"); model.addAttribute("where","广州"); model.addAttribute("gongzi","20000"); model.addAttribute("zhuangtai","找工作"); model.addAttribute("xingzhi","全职"); model.addAttribute("hangye","it行业");
    model.addAttribute("pingjia","积极向上,动手能力强,善于解决问题。");
    return "model";

    }

6 界面(乱码了,统一了编码格式还是乱码,再找找)

7 下载生成word文档(这里没有下载图片,图片存在于服务器,所以只要根据url下载图片到本地文件夹再引入就可以了)

    7 .1 生成word的方法:

/** 
     * 生成word文件 
     * @param dataMap word中需要展示的动态数据,用map集合来保存 
     * @param templateName word模板名称,例如:model.ftl 
     * @param filePath 文件生成的目标路径,例如:D:\\freemarker 
     * @param fileName 生成的文件名称,例如:Test.doc 
     */   public static void createWord(Map dataMap,String templateName,String filePath,String fileName){
        try {
            //创建配置实例
            Configuration configuration = new Configuration();

            //设置编码
            configuration.setDefaultEncoding("utf-8");

            //ftl模板文件
            configuration.setClassForTemplateLoading(FreemarkerUtil.class,"/");

            //获取模板
            Template template = configuration.getTemplate(templateName);

            //输出文件
            File outFile = new File(filePath+File.separator+fileName);

            //如果输出目标文件夹不存在,则创建
            if (!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdirs();
            }

            //将模板和数据模型合并生成文件
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));


            //生成文件
            template.process(dataMap, out);

            //关闭流
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

7.2 下载请求:

@RequestMapping("/model.action")
    public void model(){
       Map<String,Object> dataMap=new HashMap<String,Object>();
       dataMap.put("name","张三");
        dataMap.put("sex","男"); dataMap.put("age","20");dataMap.put("xueli","本科"); dataMap.put("live","湖南.衡阳"); dataMap.put("hukou","湖南.郴州");dataMap.put("tel","15570960031");
        dataMap.put("email","[email protected]"); dataMap.put("where","广州"); dataMap.put("gongzi","20000"); dataMap.put("zhuangtai","找工作"); dataMap.put("xingzhi","全职"); dataMap.put("hangye","it行业");dataMap.put("zhiye","it行业");
        dataMap.put("pingjia","积极向上,动手能力强,善于解决问题。");
        String templateName="model.ftl";
        String filePath="D:\\freemarker";
        String fileName="Test.doc";
        try {
            FreemarkerUtil.createWord(dataMap,templateName,filePath,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件下载失败");
        }
    }

8  左后在D:\\freemarker下生成了Test.doc文档(还是有乱码,再找找)。

猜你喜欢

转载自blog.csdn.net/qq_40693828/article/details/85138084