java根据xml模板生成word(编辑内容)

1.使用的依赖

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.25-incubating</version>
        </dependency>

2.Service

package com.hj.test;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import freemarker.cache.URLTemplateLoader;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

import javax.servlet.http.HttpServletRequest;

@Service
public class DynamicallyGeneratedWordService {
    private static Configuration freemarkerConfig;
    @Autowired
    HttpServletRequest req;
    static {
        freemarkerConfig = new Configuration(Configuration.VERSION_2_3_22);
        freemarkerConfig.setEncoding(Locale.getDefault(), "UTF-8");
    }



    /**
     * 生成word文档
     * @param filePath
     * @throws TemplateNotFoundException
     * @throws MalformedTemplateNameException
     * @throws ParseException
     * @throws IOException
     * @throws TemplateException
     */
    public void genWordFile(String filePath) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("topic", "基于Java模板技术动态生成Word文档");



        freemarkerConfig.setTemplateLoader(new URLTemplateLoader() {

            @Override
            protected URL getURL(String arg0) {
                //此处访问的地址是项目target/classes文件夹
                //输出的类似:E:/software/idea/aa/target/classes/6.xm
                return DynamicallyGeneratedWordService.class.getResource("/6.xml");
            }
        });

        Template temp = freemarkerConfig.getTemplate("6.xml");

        File targetFile = new File(filePath);
        Writer out = new OutputStreamWriter(new FileOutputStream(targetFile),"UTF-8");

        //执行模板替换
        temp.process(result, out);
        out.flush();
    }
}

 

xml地址:

java根据xml模板生成word(编辑内容)

3.Controller

package com.hj.test;


import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import  com.hj.test.Student;
import  com.hj.test.DynamicallyGeneratedWordService;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

/**
 * @author Solin
 * 基于Java模板技术动态生成Word文档
 */
@Controller
public class DynamicallyGeneratedWordController {
    @Autowired
    private DynamicallyGeneratedWordService dynamicallyGeneratedWordService;

    @ResponseBody
    @RequestMapping("/getStudentList")
    public String getStudentList(){
        String result = "成功";
        try {
            dynamicallyGeneratedWordService.genWordFile("d://stu.doc");
        } catch (TemplateNotFoundException e) {
            result = "失败";
            e.printStackTrace();
        } catch (MalformedTemplateNameException e) {
            result = "失败";
            e.printStackTrace();
        } catch (ParseException e) {
            result = "失败";
            e.printStackTrace();
        } catch (IOException e) {
            result = "失败";
            e.printStackTrace();
        } catch (TemplateException e) {
            result = "失败";
            e.printStackTrace();
        }
        return result;
    }
}

编辑xml文件:(xml模板可编辑word文档使用wps另存为xml格式)

java根据xml模板生成word(编辑内容)

 

java根据xml模板生成word(编辑内容)

java根据xml模板生成word(编辑内容)

参考:https://blog.****.net/qq_32786873/article/details/52535845