java生成指定样式的word文档

该博客介绍了如何使用Java结合Freemarker库生成指定样式的Word文档。步骤包括准备Word模板,将模板转换为.ftl文件,设置Java代码读取并填充数据,最后通过工具类实现文件下载。示例代码展示了数据写入和文件下载的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java生成指定样式的word文档

简要步骤

1、首先需要准备一个word模板,样式都可以在office上设置好,在需要填入值得地方用 ${name}的方式填充。
在这里插入图片描述
2、将准备好的word模板另存为 .xml 格式的文件,然后将其后缀名改为 .ftl 。文件打开后如下图
在这里插入图片描述
3、将word模板和.fil文件可以放在服务器上指定的地方,位置可以自定义。
在这里插入图片描述
在这里插入图片描述

相关代码如下

Java代码(向模板中写入数据):

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
private void wordOutput(Map<String, Object> dataMap, String sourceName, String targetPath, String
            tempaltePath) {
        try {
            // Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.23"));
            configuration.setDefaultEncoding("utf-8");
            String resourcesPath = tempaltePath;
            // 处理路径中的中文乱码问题
            String path = URLDecoder.decode(resourcesPath, "utf-8");
            System.out.println(path);
            configuration.setDirectoryForTemplateLoading(new File(path));
            // 输出文档路径及名称
            File outFile = new File(targetPath);
            // 以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate("柯桥区技术合作拟奖励项目专家评审意见表.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();
        }
    }
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>

每次写入数据都会覆盖掉之前的数据,最后下载改模板即可。
在这里插入图片描述
文件下载工具类

public void downloadfile(HttpServletRequest request, HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        String fileName = file.getName();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            request.setCharacterEncoding("UTF-8");
            String agent = request.getHeader("User-Agent").toUpperCase();
            if ((agent.indexOf("MSIE") > 0) || ((agent.contains("RV")) && (!agent.contains("FIREFOX"))))
                fileName = URLEncoder.encode(fileName, "UTF-8");
            else {
                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
            }
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            response.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

——内容仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值