From 214cda58c3786972c958da5c6d54a135490a3c11 Mon Sep 17 00:00:00 2001
From: rk <94314517@qq.com>
Date: 星期四, 05 二月 2026 09:13:16 +0800
Subject: [PATCH] 功能开发
---
server/services/src/main/java/com/doumee/core/annotation/excel/ExcelPictureUtil.java | 92 ++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 84 insertions(+), 8 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 de3c552..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;
@@ -33,7 +29,7 @@
public static Map<String, XSSFPictureData> getExcelPictures(InputStream is) {
try {
- byte[] fileData = getFileStream(is);
+ byte[] fileData = toByteArray(is);
Map<String, XSSFPictureData> pictures = getPictures(fileData);
pictures.forEach((id, xssfPictureData) -> {
System.out.println("id锛�" + id);
@@ -45,6 +41,15 @@
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();
}
/**
@@ -306,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