java-freemarker生成word文件的方法总结

本文总结了使用Java和Freemarker模板引擎生成Word文件的步骤,包括pom依赖、文件base64编码、不同格式附件处理及Word生成与doc转docx的方法。

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

1.pom依赖

<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.2.2</version>
        </dependency>

2.图片、附件等文件base64编码

	/**
     * Gets base 64 string.
     *
     * @param imageFile 图片文件的全路径名称
     * @return Base64编码的图片内容字符串 base 64 string
     * @Description 将图片内容转换成Base64编码的字符串
     */
    public static String getBase64String(String imageFile) {
   
        String imageBase64 = Constants.NOTHING_STRING;
        if (StringUtils.isEmpty(imageFile)) {
   
            return imageBase64;
        }

        return getBase64StringByFile(new File(imageFile));
    }

    /**
     * @Description 获取文件的base64编码
     *
     * @Param file 图片文件
     * @return Base64编码的图片内容字符串
     **/
    private static String getBase64StringByFile(File file) {
   
        String imageBase64 = Constants.NOTHING_STRING;
        byte[] data;
        try (InputStream inputStream = new FileInputStream(file.getCanonicalFile())) {
   
            data = new byte[in
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值