java导出word并向导出的word中添加附件

word可以转换为xml格式(另存为的时候可以选择文件类型)
在这里插入图片描述
当转换为xml格式后我们可以发现在附件位置存储的其实是附件的base64字符串。
因此,我们可以在模板中添加一个附件作为占位,而后我们将模板中占位的附件base64字符串替换为我们真正需要的字符串,就能达到添加附件的效果

下面是具体实现过程

引入pom

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

准备word模板

准备word模板(使用office的word,使用wps保存出来的xml格式与office不同,暂未测试wps的是否可行)
在这里插入图片描述
将上面的模板另存为xml格式
打开转换为xml格式的word模板,在需要动态写入的地方进行修改,使用${}占位符替代
在这里插入图片描述
附件处同样使用占位符替代
在这里插入图片描述
这里需要注意的是:附件由附件数据与在word中展示的图标组成(如下所示),我们需要替换的是数据部分。数据本分一般在后一个**pkg:binaryData**标签中
在这里插入图片描述
在修改完成后将xml的后缀改为ftl

word工具类

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;


/**
 * @author: tanghaizhi
 * @CreateTime: 2022/6/27 11:38
 * @Description:
 */
public class WordUtils {

    /**
     * 生成 word 文档方法
     *
     * @param dataMap      要填充的数据
     * @param templateName 模版名称
     * @param fileName     要输出的文件路径
     * @throws Exception 抛出的异常
     */
    public static void generateWord(Map<String, Object> dataMap, String templateName, String fileName) throws Exception {

        // 设置FreeMarker的版本和编码格式
        Configuration configuration = new Configuration(new Version("2.3.28"));
        configuration.setDefaultEncoding("UTF-8");

        // 设置FreeMarker生成Word文档所需要的模板的路径
        // configuration.setDirectoryForTemplateLoading(new File("/Users/xxx/Desktop/"));
        // 此处把模版文件都放在 resources 下的 templates 中
        configuration.setClassForTemplateLoading(WordUtils.class, "/templates");

        // 设置FreeMarker生成Word文档所需要的模板
        Template tem = configuration.getTemplate(templateName, "UTF-8");
        // 创建一个Word文档的输出流
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8));
        // FreeMarker使用Word模板和数据生成Word文档
        tem.process(dataMap, out);
        out.flush();
        out.close();
    }

}

将文件转为6ase64字符串的工具类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * @author: tanghaizhi
 * @CreateTime: 2022/6/27 11:24
 * @Description:
 */
public class Base64Util {

    public static String getBase64FromInputStream(InputStream in) {
        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = in.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Base64.getEncoder().encodeToString(data);
    }

}

进行测试生成

public static void main(String[] args) throws Exception {
    Map<String,Object> dataMap = new HashMap<>();
    String str = Base64Util.getBase64FromInputStream(new FileInputStream("D://ceshi.docx"));
    dataMap.put("file1",str);
    dataMap.put("flag","那必须是");
    String templateName = "demo.ftl";
    //这里注意一定得是doc,用doc可能会出现wps可以打开但是office打不开的情况,具体原因未知
    WordUtils.generateWord(dataMap, templateName, "D://demo.doc");
}

可以看到模板中占位符已被替换为我们输入的字符串
在这里插入图片描述
双击文档中的附件,打开附件可以发现,附件也已被替换为我们指定的文件(附件图标上的名字虽然还是原来的,但那只是在word文档中展示的)
在这里插入图片描述

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值