Java使用freemarker导出word试卷

使用了freemarker模板填充占位符的方式来生成word文档,支持跨平台。

Java导出Word的五种方式

制作Word模板

将制作好的Word模板.docx另存为.xml格式,修改xml文件的内容符合freemarker解析规范;并将xml文件的后缀名改为.ftl就行了。

创建Word模板具体过程可参考:使用FreeMarker自动生成Word文档

环境

Java 8
SpringBoot 2.3.10

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

Word操作工具类

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @description: word工具类
 */
@Component
public class DocUtil {

    private Configuration configuration = null;

    public DocUtil() {
        // 设置版本号
        configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setDefaultEncoding("UTF-8");
    }

    /**
     * 生成插入数据后的Word文件
     *
     * @param filename:目标文件名(要带上后缀名)
     * @param reportTemplate         模板名称
     * @param response:
     * @param data:要写入的数据
     */
    public void exportReport(String filename, String reportTemplate, HttpServletResponse response, Map<String, Object> data) {
        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;

        try {
            // 获取写入数据后的文件
            File outFile = writeReport(reportTemplate, filename, data);

            response.reset();
            response.setCharacterEncoding("UTF-8");
            // 设置内容类型为Word格式
            response.setContentType("application/msword");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            response.setContentType("application/octet-stream");

            inputStream = new BufferedInputStream(new FileInputStream(outFile));
            outputStream = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[inputStream.available() + 1024];
            int num = 0;
            while ((num = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, num);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param templateName:模板文件名
     * @param filename:目标文件名
     * @param data:要写入模板文件的数据
     */
    public File writeReport(String templateName, String filename, Map<String, Object> data) {
        // 设置模板加载路径
        configuration.setClassForTemplateLoading(this.getClass(), "/template");
        // 允许为 null
        configuration.setClassicCompatible(true);
        File outFile = new File(filename);
        try {
            Template template = configuration.getTemplate(templateName);
            Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8));
            // 将数据填充到模板并写入目标文件
            template.process(data, writer);
            writer.close();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        return outFile;
    }
}

接下来的操作就是查询数据并处理,然后调用工具类的方法就行了。

注意:

  1. Map中的Key必须和模板中的对应,否则会报错。

  2. Word试卷的题目、选项是用表格(表格的边框设置不可见)模板的。

Word试卷模板设置可参考:Java用freemarker导出word

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值