easyExcel读取图片
时间: 2023-07-18 10:04:56 浏览: 464
EasyExcel 是一个用于操作 Excel 文件的 Java 库,它不支持直接读取 Excel 中的图片。但是你可以通过以下方式实现读取图片:
1. 使用 EasyExcel 读取 Excel 文件,获取图片所在单元格的位置信息。
```java
List<Object> row = excelReader.readRow(sheetNo, rowIndex);
```
2. 使用 Apache POI 库读取 Excel 文件,获取图片对象。
```java
Workbook workbook = WorkbookFactory.create(new File(filePath));
Sheet sheet = workbook.getSheetAt(sheetIndex);
Drawing drawing = sheet.getDrawingPatriarch();
PictureData pictureData = null;
for (Shape shape : drawing.getShapes()) {
if (shape instanceof Picture) {
Picture picture = (Picture) shape;
ClientAnchor anchor = picture.getClientAnchor();
if (anchor.getRow1() == rowIndex && anchor.getCol1() == colIndex) {
pictureData = picture.getPictureData();
break;
}
}
}
```
3. 将图片对象写入到本地文件或者输出流中。
```java
FileOutputStream outputStream = new FileOutputStream(new File("image.png"));
outputStream.write(pictureData.getData());
outputStream.close();
```
注意:使用 Apache POI 库需要引入以下依赖:
```xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
```
阅读全文