From cb94ff4f0a46eb74c21dd7b34b8d958ef9a9f11c Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期一, 02 二月 2026 09:05:28 +0800
Subject: [PATCH] Merge branch 'master' of http://139.186.142.91:10010/r/productDev/zbom_dianjiang
---
server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java | 112 ++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 96 insertions(+), 16 deletions(-)
diff --git a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java
index 08b073c..9d9ec0c 100644
--- a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java
+++ b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java
@@ -7,17 +7,13 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -32,14 +28,28 @@
public class ExcelPictureUtil {
public static Map<String, XSSFPictureData> getExcelPictures(InputStream is) {
- byte[] fileData = getFileStream(is);
- Map<String, XSSFPictureData> pictures = getPictures(fileData);
- pictures.forEach((id, xssfPictureData) -> {
- System.out.println("id锛�" + id);
- String fileName = xssfPictureData.getPackagePart().getPartName().getName();
- System.out.println("fileName锛�" + fileName);
- });
- return pictures;
+ try {
+ byte[] fileData = toByteArray(is);
+ Map<String, XSSFPictureData> pictures = getPictures(fileData);
+ pictures.forEach((id, xssfPictureData) -> {
+ System.out.println("id锛�" + id);
+ String fileName = xssfPictureData.getPackagePart().getPartName().getName();
+ System.out.println("fileName锛�" + fileName);
+ });
+ return pictures;
+ }catch (Exception e){
+ return null;
+ }
+
+ }
+ public static byte[] toByteArray(InputStream input) throws IOException {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ int bytesRead;
+ byte[] buffer = new byte[1024]; // 鍙互鏍规嵁闇�瑕佽皟鏁寸紦鍐插尯澶у皬
+ while ((bytesRead = input.read(buffer)) != -1) {
+ byteArrayOutputStream.write(buffer, 0, bytesRead);
+ }
+ return byteArrayOutputStream.toByteArray();
}
/**
@@ -289,7 +299,6 @@
// 鍒涘缓 ByteArrayOutputStream 鏉ユ殏瀛樻祦鏁版嵁
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- // 灏� inputStream 璇诲彇鍒� byteArrayOutputStream 涓�
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
@@ -302,4 +311,75 @@
}
}
+
+ /**
+ * 鑾峰彇鍗曞厓鏍间腑鐨勫浘鐗�
+ */
+
+ public static List<XSSFPictureData> getPicturesFromCell(Sheet sheet, Cell cell) {
+ try {
+ List<XSSFPictureData> cellPictures = new ArrayList<>();
+ XSSFDrawing drawing = ((XSSFSheet) sheet).createDrawingPatriarch();
+ if (drawing != null) {
+ List<XSSFPicture> pictures = drawing.getShapes().stream()
+ .filter(shape -> shape instanceof XSSFPicture)
+ .map(shape -> (XSSFPicture) shape).collect(Collectors.toList());
+ for (XSSFPicture picture : pictures) {
+ int rowIndex = picture.getPreferredSize().getRow1();
+ int colIndex = picture.getPreferredSize().getCol1();
+ if (colIndex == cell.getColumnIndex() && rowIndex == cell.getRowIndex()) {
+ cellPictures.add(picture.getPictureData());
+ }
+ }
+ }
+ return cellPictures;
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private static Map<String, List<XSSFPicture>> getImgPathMap(Sheet sheet) {
+ Map<String, List<XSSFPicture>> imgPathMap = new HashMap<>();
+ XSSFDrawing drawing = ((XSSFSheet) sheet).createDrawingPatriarch();
+ if (drawing != null) {
+ List<XSSFPicture> pictures = drawing.getShapes().stream()
+ .filter(shape -> shape instanceof XSSFPicture)
+ .map(shape -> (XSSFPicture) shape).collect(Collectors.toList());
+ for (XSSFPicture picture : pictures) {
+ int rowIndex = picture.getPreferredSize().getRow1();
+ int colIndex = picture.getPreferredSize().getCol1();
+ String key = colIndex + "-" + rowIndex;
+ List<XSSFPicture> list = null;
+ if (imgPathMap.containsKey(key)) {
+ list = imgPathMap.get(key);
+ } else {
+ list = new ArrayList<>();
+ imgPathMap.put(key, list);
+ }
+ list.add(picture);
+ }
+ }
+ return imgPathMap;
+ }
+
+
+ /**
+ * 鑾峰彇鍥剧墖鐨勬墿灞曞悕
+ */
+
+ private static String getImageExtension(PictureData picture) {
+ String mimeType = picture.getMimeType();
+ switch (mimeType) {
+ case "image/jpeg":
+ return "jpg";
+ case "image/png":
+ return "png";
+ default:
+ return "";
+ }
+ }
+
+
+
}
\ No newline at end of file
--
Gitblit v1.9.3