From 2815206d8821a79de11d6f2c3de9c4bedbe719b0 Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期五, 30 一月 2026 15:29:17 +0800
Subject: [PATCH] 提交忽略文件
---
server/services/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java | 176 +++++++++++++++++++++++++++++++++++++++-------------------
1 files changed, 118 insertions(+), 58 deletions(-)
diff --git a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java
index f8f067c..a19134d 100644
--- a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java
+++ b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java
@@ -4,15 +4,14 @@
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFPicture;
+import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
@@ -42,7 +41,7 @@
* 鏍囬琛屽彿
*/
private CellType changeType;
-
+ private Map<String, XSSFPictureData> pictureList;
/**
* 鏋勯�犲嚱鏁�
* @param--path 瀵煎叆鏂囦欢锛岃鍙栫涓�涓伐浣滆〃
@@ -146,12 +145,13 @@
}
public ExcelImporter(String fileName, InputStream in, int headerNum, int sheetIndex,CellType cellType)
throws InvalidFormatException, IOException {
+ ByteArrayOutputStream buffer = toByteArray(in);
if (StringUtils.isBlank(fileName)){
throw new RuntimeException("瀵煎叆鏂囨。涓虹┖!");
}else if(fileName.toLowerCase().endsWith("xls")){
- this.wb = new HSSFWorkbook(in);
+ this.wb = new HSSFWorkbook(new ByteArrayInputStream(buffer.toByteArray()));
}else if(fileName.toLowerCase().endsWith("xlsx")){
- this.wb = new XSSFWorkbook(in);
+ this.wb = new XSSFWorkbook(new ByteArrayInputStream(buffer.toByteArray()));
}else{
throw new RuntimeException("鏂囨。鏍煎紡涓嶆纭�!");
}
@@ -161,9 +161,27 @@
this.sheet = this.wb.getSheetAt(sheetIndex);
this.headerNum = headerNum;
this.changeType = cellType;
+ this.pictureList = ExcelPictureUtil.getExcelPictures(new ByteArrayInputStream(buffer.toByteArray()));
log.debug("Initialize success.");
}
+ public static ByteArrayOutputStream toByteArray(InputStream inputStream) {
+ try {
+ // 缂撳瓨鏂囦欢娴�
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ byte[] data = new byte[1024];
+ int nRead;
+ while ((nRead = inputStream.read(data, 0, data.length))!= -1) {
+ buffer.write(data, 0, nRead);
+ }
+ buffer.flush();
+
+ return buffer;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
/**
@@ -227,6 +245,9 @@
return val;
}
return val;
+ }
+ public Map<String,XSSFPictureData> getExcelPictures(){
+ return this.pictureList;
}
/**
@@ -309,63 +330,102 @@
}
StringBuilder sb = new StringBuilder();
for (Object[] os : annotationList){
- Object val = this.getCellValue(row, column++);
- if (val != null){
- ExcelColumn ef = (ExcelColumn)os[0];
- // Get param type and type cast
- Class<?> valType = Class.class;
- if (os[1] instanceof Field){
- valType = ((Field)os[1]).getType();
- }else if(os[1] instanceof Method){
- Method method = ((Method)os[1]);
- if ("get".equals(method.getName().substring(0, 3))){
- valType = method.getReturnType();
- }else if("set".equals(method.getName().substring(0, 3))){
- valType = ((Method)os[1]).getParameterTypes()[0];
- }
+ int tcolumn = column++;
+ Object val = this.getCellValue(row, tcolumn);;
+ ExcelColumn ef = (ExcelColumn)os[0];
+ // Get param type and type cast
+ Class<?> valType = Class.class;
+ if (os[1] instanceof Field){
+ valType = ((Field)os[1]).getType();
+ }else if(os[1] instanceof Method){
+ Method method = ((Method)os[1]);
+ if ("get".equals(method.getName().substring(0, 3))){
+ valType = method.getReturnType();
+ }else if("set".equals(method.getName().substring(0, 3))){
+ valType = ((Method)os[1]).getParameterTypes()[0];
}
- //log.debug("Import value type: ["+i+","+column+"] " + valType);
- try {
- if (valType == String.class){
- String s = String.valueOf(val.toString());
- if(StringUtils.endsWith(s, ".0")){
- val = StringUtils.substringBefore(s, ".0");
- }else{
- val = String.valueOf(val.toString()).trim();
+ }
+ if(ef.fieldType()== XSSFPictureData.class){
+ XSSFPictureData qrData = null;
+ if(val !=null){
+ String tid = String.valueOf(val.toString()).trim();
+ int start = tid.indexOf("(\"")+2;
+ int end = tid.indexOf("\",");
+ if(start>=0&& end>=1 && end>start){
+ String picId = tid.substring(start,end);
+ XSSFPictureData data = this.pictureList.get(picId);
+ if(data!= null && data.getData() != null){
+ qrData =data;
}
}
- else if (valType == Integer.class){
- val = Double.valueOf(val.toString()).intValue();
- }else if (valType == Long.class){
- val = Double.valueOf(val.toString()).longValue();
- }else if (valType == Double.class){
- val = Double.valueOf(val.toString());
- }else if (valType == Float.class){
- val = Float.valueOf(val.toString());
- }else if (valType == Date.class){
- val = DateUtil.getJavaDate((Double)val);
- }else{
- if (ef.fieldType() != Class.class){
- val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
- }else{
- val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
- "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
+ }
+ val = null;
+ //鎮诞鍥剧墖
+ List<XSSFPictureData> xfData = ExcelPictureUtil.getPicturesFromCell(this.sheet,row.getCell(tcolumn));
+ List<XSSFPictureData> valList =new ArrayList<>();
+ if(qrData!=null){
+ valList.add(qrData);
+ }
+ if(xfData!=null){
+ valList.addAll(xfData);
+ }
+ if (valType==List.class){
+ val =valList;
+ } else if ( valType==XSSFPictureData.class){
+ val = (valList!=null && valList.size()>0)?valList.get(0):null;
+ }
+ }else{
+ if (val != null){
+ try {
+ if (valType == String.class){
+ String s = String.valueOf(val.toString());
+ if(StringUtils.endsWith(s, ".0")){
+ val = StringUtils.substringBefore(s, ".0");
+ }else{
+ val = String.valueOf(val.toString()).trim();
+ }
}
+ else if (valType == Integer.class){
+ val = Double.valueOf(val.toString()).intValue();
+ }else if (valType == Long.class){
+ val = Double.valueOf(val.toString()).longValue();
+ }else if (valType == Double.class){
+ val = Double.valueOf(val.toString());
+ }else if (valType == Float.class){
+ val = Float.valueOf(val.toString());
+ }else if (valType == Date.class){
+ val = DateUtil.getJavaDate((Double)val);
+ }else if (valType == XSSFPictureData.class){
+ }else{
+ if (ef.fieldType() != Class.class){
+ val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
+ }else{
+ val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
+ "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
+ }
+ }
+ } catch (Exception ex) {
+ log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
+ val = null;
}
- } catch (Exception ex) {
- log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
- val = null;
}
- // set entity value
- if (os[1] instanceof Field){
- Reflections.invokeSetter(e, ((Field)os[1]).getName(), val);
- }else if (os[1] instanceof Method){
- String mthodName = ((Method)os[1]).getName();
- if ("get".equals(mthodName.substring(0, 3))){
- mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
- }
- Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
- }
+ }
+ if (val != null){
+ try {
+ // set entity value
+ if (os[1] instanceof Field){
+ Reflections.invokeSetter(e, ((Field)os[1]).getName(), val);
+ }else if (os[1] instanceof Method){
+ String mthodName = ((Method)os[1]).getName();
+ if ("get".equals(mthodName.substring(0, 3))){
+ mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
+ }
+ Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
+ }
+ }catch (Exception e1){
+ val =null;
+ }
+ }else{
}
sb.append(val+", ");
}
--
Gitblit v1.9.3