现在有个excel存在多行数据,每行都包含图片(图片都是嵌入到单元格的,每条数据一一对应),现在要通过easyExcel读取这个excel,其中读取到的图片数据需要上传到服务器返回地址,该怎么写代码?(excel文件包含.xls和.xlsx)
5条回答 默认 最新
檀越@新空间 2025-07-22 14:35关注下午好🌅🌅🌅
本答案参考通义千问要通过 EasyExcel 读取 Excel 文件(支持
.xls和.xlsx格式),并从中提取嵌入到单元格中的图片,然后将这些图片上传到服务器并返回地址,可以按照以下步骤进行操作。
✅ 关键点说明
- EasyExcel 主要用于读写表格数据,但不直接支持读取 Excel 中的图片。
- 需要使用 Apache POI 来处理 Excel 文件中的图片(因为 EasyExcel 底层也基于 POI)。
- 图片是嵌入到单元格中的,所以需要从单元格中提取图像资源。
- 提取图片后,将其转换为
byte[]或MultipartFile,然后上传到服务器并获取返回地址。
🧠 解决方案步骤
-
引入依赖
确保项目中包含easyexcel、poi和poi-ooxml依赖(适用于.xlsx)和poi-scratchpad(适用于.xls)。 -
读取 Excel 文件并提取图片
使用 Apache POI 读取 Excel 文件,并遍历每个工作表和行,提取嵌入的图片。 -
上传图片到服务器
将提取的图片以byte[]形式上传至服务器,获取返回的图片地址。 -
将图片地址与数据关联
在读取每条数据时,同时保存对应的图片地址。
📦 Maven 依赖(pom.xml)
<dependencies> <!-- EasyExcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.3.2</version> </dependency> <!-- Apache POI for .xlsx --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> <!-- Apache POI for .xls --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.3</version> </dependency> <!-- For .xls support --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>5.2.3</version> </dependency> </dependencies>
📄 Java 实现代码
1. 定义 Excel 数据模型类
public class ExcelData { private String text; private String imageUrl; // Getters and Setters public String getText() { return text; } public void setText(String text) { this.text = text; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } }
2. 自定义 Excel 读取处理器(结合 POI)
import org.apache.poi.ss.usermodel.*; import org.apache.poi.util.IOUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.client.RestTemplate; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class ExcelImageReader { public static List<ExcelData> readExcelWithImages(String filePath) throws Exception { List<ExcelData> dataList = new ArrayList<>(); Workbook workbook = null; try (InputStream inputStream = new FileInputStream(filePath)) { if (filePath.endsWith(".xls")) { workbook = new HSSFWorkbook(inputStream); } else if (filePath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(inputStream); } else { throw new IllegalArgumentException("Unsupported file format"); } for (Sheet sheet : workbook) { for (Row row : sheet) { ExcelData data = new ExcelData(); data.setText(row.getCell(0).getStringCellValue()); // 提取图片 List<org.apache.poi.ddf.EscherRecord> escherRecords = getEscherRecords(sheet, row); for (org.apache.poi.ddf.EscherRecord record : escherRecords) { if (record instanceof org.apache.poi.ddf.EscherPictureBlob) { org.apache.poi.ddf.EscherPictureBlob pictureBlob = (org.apache.poi.ddf.EscherPictureBlob) record; byte[] imageBytes = pictureBlob.getPictureData(); String imageUrl = uploadImageToServer(imageBytes); data.setImageUrl(imageUrl); } } dataList.add(data); } } } finally { if (workbook != null) { workbook.close(); } } return dataList; } private static List<org.apache.poi.ddf.EscherRecord> getEscherRecords(Sheet sheet, Row row) { List<org.apache.poi.ddf.EscherRecord> records = new ArrayList<>(); int rowIndex = row.getRowNum(); int cellIndex = row.getCell(0).getCellNum(); Drawing<?> drawing = sheet.createDrawingPatriarch(); for (int i = 0; i < drawing.getNumberOfShapes(); i++) { Shape shape = drawing.getShape(i); if (shape instanceof Picture) { Picture picture = (Picture) shape; ClientAnchor anchor = picture.getAnchor(); if (anchor.getRow1() == rowIndex && anchor.getColumn1() == cellIndex) { records.addAll(picture.getEscherRecords()); } } } return records; } private static String uploadImageToServer(byte[] imageBytes) { // 这里模拟上传逻辑,实际应替换为真实上传方法 RestTemplate restTemplate = new RestTemplate(); String serverUrl = "http://your-server.com/upload"; MultipartFile multipartFile = new MockMultipartFile( "file", "image.jpg", "image/jpeg", imageBytes); // 假设服务端返回图片地址 String response = restTemplate.postForObject(serverUrl, multipartFile, String.class); return response; } }
3. 调用示例
public class Main { public static void main(String[] args) { try { List<ExcelData> data = ExcelImageReader.readExcelWithImages("path/to/your/excel.xlsx"); for (ExcelData item : data) { System.out.println("Text: " + item.getText()); System.out.println("Image URL: " + item.getImageUrl()); } } catch (Exception e) { e.printStackTrace(); } } }
⚠️ 注意事项
- 图片位置匹配:确保图片确实嵌入在对应的数据单元格中,否则可能无法正确识别。
- 文件格式兼容性:
.xls和.xlsx的处理方式略有不同,需分别处理。 - 图片格式支持:POI 支持大部分常见图片格式(如 JPG、PNG、BMP)。
- 服务器上传接口:你需要根据实际的服务器接口修改
uploadImageToServer()方法。
✅ 总结
| 步骤 | 内容 | |------|------| | 1 | 引入必要的 Maven 依赖 | | 2 | 使用 Apache POI 读取 Excel 并提取图片 | | 3 | 将图片转为
byte[]或MultipartFile| | 4 | 上传图片到服务器并获取地址 | | 5 | 将图片地址与数据绑定 |如果你有具体的服务器上传接口或图片存储方式,我可以进一步帮你优化代码。
解决 无用评论 打赏 举报