JAVA实现向Word模板中插入Base64图片和数据信息

目录

需求

在服务端提前准备好Word模板文件,并在用户请求接口时服务端动态获取图片。数据等信息插入到模板当中,然后返回包含数据信息的Word文件流。

一、准备模板文件

在需要插入图片的地方使用:{{@参数名}},文本信息使用:{{参数名}},进行占位,占位格式将会被保留,经过处理后格式不变

将准备好的模板文件放在resources目录下

二、引入Poi-tl、Apache POI依赖

poi-tl(poi template language)是Word模板引擎,基于Apache POI,提供更友好的API,使用起来更加简单

版本对应关系参考Poi-tl官网

java 复制代码
<!--    替换自己使用的版本    -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>4.1.*</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>4.1.*</version>
</dependency>
<!--    Word模板引擎    -->
<dependency>
	<groupId>com.deepoove</groupId>
	<artifactId>poi-tl</artifactId>
	<version>1.7.*</version>
</dependency>

三、创建实体类(用于保存向Word中写入的数据)

参数名必须同Word模板中的参数名称保持一致

java 复制代码
import com.deepoove.poi.data.PictureRenderData;

@Data
public class DownloadDate {
    //图片使用PictureRenderData类型
    private PictureRenderData image;
    
    private String name;

    private String a;

    private String b;

    private String c;

    private String d;

    private String e;

    private String f;

    private String g;

    private String h;
    
    private String i;
}

四、实现Service接口

java 复制代码
public interface DownloadService {
     void download(HttpServletResponse response, DownloadDTO downloadDTO) throws IOException;
}
java 复制代码
@Service
@Slf4j
public class DownloadServiceImpl implements DownloadService {

    @Resource
    //远程调用服务
    private FeignService feignService;
    
    @Override
    public void download(HttpServletResponse response, DownloadDTO downloadDTO) throws IOException {BufferedImage、字节数组),Base64可以转字节数组后使用
    	//通过调用其它接口获取待写入的数据信息
    	WordData wordData = feignService.getData(downloadDTO);
        /** 
        * 图片可以是多种格式------------------------
        * 图片路径:PictureRenderData(int width, int height, String path)
        * File:PictureRenderData(int width, int height, File picture)
        * InputStream:PictureRenderData(int width, int height, String format, InputStream input)
        * BufferedImage:PictureRenderData(int width, int height, String format, BufferedImage image)
        * 字节数组:PictureRenderData(int width, int height, String format, byte[] data)
        * Base64可以转字节数组后使用
        */

        //以Base64为例,先获取图片的Base64编码(wordData.getImg是原始图片Base64数据)
        String base64ImageData = wordData.getImg.substring(data.indexOf(",") + 1);
        //获取图片类型
        String format = getBase64Type(base64ImageData);
        // 将base64数据转为字节数组
        byte[] imageBytes = Base64.getDecoder().decode(base64ImageData);
        // 将字节数组包装成PictureRenderData
        PictureRenderData pictureRenderData = new PictureRenderData(690,530,format,imageBytes);
        //待写入Word的数据
        DownloadDate downloadDate = new DownloadDate();
        //图片信息
        downloadDate.setImage(pictureRenderData);
        //其它信息
        downloadDate.setCompany(wordData.getCompany());
        //...
        XWPFTemplate template = null;
        BufferedOutputStream bufferedOutputStream = null;
        ServletOutputStream outputStream = null;
        try {
            //获得resource路径+模板路径
            String path = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath() + "word/template.docx";
            // 读取Word模板
            FileInputStream templateInputStream = new FileInputStream(path);
            // 模板绑定数据
            template = XWPFTemplate.compile(templateInputStream).render(imageDownloadDate);
            //文件名
            String encodedFileName = URLEncoder.encode(System.currentTimeMillis()+"", "utf-8");
            //设置响应信息
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".docx");
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setCharacterEncoding("utf-8");
            outputStream = response.getOutputStream();
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            template.write(bufferedOutputStream);
            //清空流
            bufferedOutputStream.flush();
            outputStream.flush();
        } catch (Exception e) {
            log.info(e.getMessage());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            //关闭资源
            PoitlIOUtils.closeQuietlyMulti(template, bufferedOutputStream, outputStream);
        }
    }
    
    //根据base64编码获取图片格式信息
    private String getBase64Type(String base64) {
        byte[] b = Base64.getDecoder().decode(base64);
        String type = ".png";
        if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
            type = ".bmp";
        } else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
            type = ".png";
        } else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
            type = ".jpg";
        } else if (0x49492A00 == ((b[0] & 0xff) << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 | (b[3] & 0xff))) {
            type = ".tif";
        }
        return type;
    }

}

Controller层实现

java 复制代码
@RestController
@RequestMapping("/test")
@Api(tags = "获取商品图片")
public class GetImageController {

    @Resource
    DownloadService downloadService;

    @PostMapping("/download")
    @ApiOperation(value = "下载Word")
    void download(HttpServletResponse response,@RequestBody DownloadDTO downloadDTO) throws IOException {
        //鉴权或其它处理
        //....
        downloadService.download(response,downloadDTO);
    }
    
}
相关推荐
某空_34 分钟前
【Android】使用ViewPager2实现简单的轮播图
java
武子康35 分钟前
Java-145 深入浅出 MongoDB 基本操作详解:数据库查看、切换、创建集合与删除完整教程
java·数据库·sql·mysql·mongodb·性能优化·系统架构
white-persist43 分钟前
XXE 注入漏洞全解析:从原理到实战
开发语言·前端·网络·安全·web安全·网络安全·信息可视化
练习时长一年1 小时前
Spring内置功能
java·前端·spring
铉铉这波能秀1 小时前
如何在Android Studio中使用Gemini进行AI Coding
android·java·人工智能·ai·kotlin·app·android studio
_Yoke1 小时前
Java 枚举多态在系统中的实战演进:从枚举策略到自动注册
java·springboot·策略模式
人生导师yxc1 小时前
Java中Mock的写法
java·开发语言
半路程序员1 小时前
Go语言学习(四)
开发语言·学习·golang
青岛少儿编程-王老师2 小时前
CCF编程能力等级认证GESP—C++5级—20250927
java·数据结构·c++
毕设源码-郭学长2 小时前
【开题答辩全过程】以 办公管理系统为例,包含答辩的问题和答案
java·eclipse