使用Hutool插入图片到Excel
pom引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.3</version>
</dependency>
</dependencies>
</project>
实现
package com.example.demo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.freemarke.TestBean;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.jupiter.api.Test;
import java.util.List;
/**
* @author ZhiGang.Jie
* @date 2022/1/4 17:10
*/
public class TestBeanToExcel {
@Test
public void writePicTest() {
TestBean bean1 = new TestBean();
bean1.setName("张三");
bean1.setAge(22);
bean1.setPass(true);
bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date());
bean1.setImgPath("./resume.png");
TestBean bean2 = new TestBean();
bean2.setName("李四");
bean2.setAge(28);
bean2.setPass(false);
bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date());
bean2.setImgPath("./user.png");
List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("./writeBeanTest.xlsx");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(5, "一班成绩单");
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(rows, true);
for (int i = 0; i < rows.size(); i++) {
//读取图片
byte[] pictureData = FileUtil.readBytes(rows.get(i).getImgPath());
//写入图片
writePic(writer, 5, i+2, pictureData, HSSFWorkbook.PICTURE_TYPE_JPEG);
}
// 关闭writer,释放内存
writer.close();
}
/**
* @param writer
* @param x 单元格x轴坐标
* @param y 单元格y轴坐标
* @param pictureData 图片二进制数据
* @param picType 图片格式
*/
private void writePic(ExcelWriter writer, int x, int y, byte[] pictureData, int picType) {
Sheet sheet = writer.getSheet();
Drawing drawingPatriarch = sheet.createDrawingPatriarch();
//设置图片单元格位置
ClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 0, 0, x, y, x + 1, y + 1);
//随单元格改变位置和大小
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
//添加图片
int pictureIndex = sheet.getWorkbook().addPicture(pictureData, picType);
drawingPatriarch.createPicture(anchor, pictureIndex);
}
}