|
|
@@ -76,38 +76,70 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
* @param params
|
|
|
*/
|
|
|
private void addListContinue(Object object, ExcelCollectionParams param, Row row, Map<Integer, String> titlemap, String targetId, Map<String, PictureData> pictures, ImportParams params) throws Exception {
|
|
|
- Collection collection = (Collection) PoiPublicUtil.getMethod(param.getName(), object.getClass()).invoke(object, new Object[] {});
|
|
|
+ Collection collection = (Collection) PoiPublicUtil.getMethod(param.getName(), object.getClass()).invoke(object, new Object[]{});
|
|
|
Object entity = PoiPublicUtil.createObject(param.getType(), targetId);
|
|
|
String picId;
|
|
|
boolean isUsed = false;// 是否需要加上这个对象
|
|
|
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
|
|
|
- Cell cell = row.getCell(i);
|
|
|
- String titleString = (String) titlemap.get(i);
|
|
|
- if (param.getExcelParams().containsKey(titleString)) {
|
|
|
- if (param.getExcelParams().get(titleString).getType() == 2) {
|
|
|
- picId = row.getRowNum() + "_" + i;
|
|
|
- saveImage(object, picId, param.getExcelParams(), titleString, pictures, params);
|
|
|
- } else {
|
|
|
- saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row);
|
|
|
+
|
|
|
+ Cell cell = row.getCell(i);
|
|
|
+ String titleString = (String) titlemap.get(i);
|
|
|
+ if (param.getExcelParams().containsKey(titleString)) {
|
|
|
+ if (param.getExcelParams().get(titleString).getType() == 2) {
|
|
|
+ picId = row.getRowNum() + "_" + i;
|
|
|
+ saveImage(object, picId, param.getExcelParams(), titleString, pictures, params);
|
|
|
+ } else {
|
|
|
+ saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row);
|
|
|
+ }
|
|
|
+ isUsed = true;
|
|
|
}
|
|
|
- isUsed = true;
|
|
|
}
|
|
|
- }
|
|
|
+
|
|
|
if (isUsed) {
|
|
|
collection.add(entity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static boolean isEmptyRow(Row row) {
|
|
|
+ //行不存在
|
|
|
+ if (row == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ //第一个列位置
|
|
|
+ int firstCellNum = row.getFirstCellNum();
|
|
|
+ //最后一列位置
|
|
|
+ int lastCellNum = row.getLastCellNum();
|
|
|
+ //空列数量
|
|
|
+ int nullCellNum = 0;
|
|
|
+ for (int c = firstCellNum; c < lastCellNum; c++) {
|
|
|
+ Cell cell = row.getCell(c);
|
|
|
+ if (null == cell || CellType.BLANK == cell.getCellType()) {
|
|
|
+ nullCellNum++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ cell.setCellType(CellType.STRING);
|
|
|
+ String cellValue = cell.getStringCellValue().trim();
|
|
|
+ if (StringUtils.isEmpty(cellValue)) {
|
|
|
+ nullCellNum++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //所有列都为空
|
|
|
+ if (nullCellNum == (lastCellNum - firstCellNum)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取key的值,针对不同类型获取不同的值
|
|
|
*
|
|
|
- * @Author JEECG
|
|
|
- * @date 2013-11-21
|
|
|
* @param cell
|
|
|
* @return
|
|
|
+ * @Author JEECG
|
|
|
+ * @date 2013-11-21
|
|
|
*/
|
|
|
private String getKeyValue(Cell cell) {
|
|
|
- if(cell==null){
|
|
|
+ if (cell == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Object obj = null;
|
|
|
@@ -147,6 +179,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
}
|
|
|
return excelImportEntity.getSaveUrl();
|
|
|
}
|
|
|
+
|
|
|
//update-begin--Author:xuelin Date:20171205 for:TASK #2098 【excel问题】 Online 一对多导入失败--------------------
|
|
|
private <T> List<T> importExcel(Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params, Map<String, PictureData> pictures) throws Exception {
|
|
|
List collection = new ArrayList();
|
|
|
@@ -189,7 +222,11 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象
|
|
|
//update-begin--Author:xuelin Date:20171206 for:TASK #2451 【excel导出bug】online 一对多导入成功, 但是现在代码生成后的一对多online导入有问题了
|
|
|
Cell keyIndexCell = row.getCell(params.getKeyIndex());
|
|
|
- if (excelCollection.size()>0 && StringUtils.isEmpty(getKeyValue(keyIndexCell)) && object != null && !Map.class.equals(pojoClass)) {
|
|
|
+ boolean emptyRow = isEmptyRow(row);
|
|
|
+ if(emptyRow){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (excelCollection.size() > 0 && StringUtils.isEmpty(getKeyValue(keyIndexCell)) && object != null && !Map.class.equals(pojoClass)) {
|
|
|
//update-end--Author:xuelin Date:20171206 for:TASK #2451 【excel导出bug】online 一对多导入成功, 但是现在代码生成后的一对多online导入有问题了
|
|
|
for (ExcelCollectionParams param : excelCollection) {
|
|
|
addListContinue(object, param, row, titlemap, targetId, pictures, params);
|
|
|
@@ -200,12 +237,12 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
try {
|
|
|
//update-begin-author:taoyan date:20200303 for:导入图片
|
|
|
int firstCellNum = row.getFirstCellNum();
|
|
|
- if(firstCellNum>minColumnIndex){
|
|
|
+ if (firstCellNum > minColumnIndex) {
|
|
|
firstCellNum = minColumnIndex;
|
|
|
}
|
|
|
int lastCellNum = row.getLastCellNum();
|
|
|
- if(lastCellNum<maxColumnIndex+1){
|
|
|
- lastCellNum = maxColumnIndex+1;
|
|
|
+ if (lastCellNum < maxColumnIndex + 1) {
|
|
|
+ lastCellNum = maxColumnIndex + 1;
|
|
|
}
|
|
|
for (int i = firstCellNum, le = lastCellNum; i < le; i++) {
|
|
|
Cell cell = row.getCell(i);
|
|
|
@@ -224,16 +261,16 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
picId = row.getRowNum() + "_" + i;
|
|
|
saveImage(object, picId, excelParams, titleString, pictures, params);
|
|
|
} else {
|
|
|
- if(params.getImageList()!=null && params.getImageList().contains(titleString)){
|
|
|
+ if (params.getImageList() != null && params.getImageList().contains(titleString)) {
|
|
|
if (pictures != null) {
|
|
|
picId = row.getRowNum() + "_" + i;
|
|
|
PictureData image = pictures.get(picId);
|
|
|
- if(image!=null){
|
|
|
+ if (image != null) {
|
|
|
byte[] data = image.getData();
|
|
|
params.getDataHanlder().setMapValue((Map) object, titleString, data);
|
|
|
}
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
saveFieldValue(params, object, cell, excelParams, titleString, row);
|
|
|
}
|
|
|
//update-end-author:taoyan date:20200303 for:导入图片
|
|
|
@@ -245,7 +282,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
addListContinue(object, param, row, titlemap, targetId, pictures, params);
|
|
|
}
|
|
|
//update-begin-author:taoyan date:20210526 for:autopoi导入excel 如果单元格被设置边框,即使没有内容也会被当做是一条数据导入 #2484
|
|
|
- if(isNotNullObject(pojoClass, object)){
|
|
|
+ if (isNotNullObject(pojoClass, object)) {
|
|
|
collection.add(object);
|
|
|
}
|
|
|
//update-end-author:taoyan date:20210526 for:autopoi导入excel 如果单元格被设置边框,即使没有内容也会被当做是一条数据导入 #2484
|
|
|
@@ -260,18 +297,20 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
return collection;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 判断当前对象不是空
|
|
|
+ *
|
|
|
* @param pojoClass
|
|
|
* @param object
|
|
|
* @return
|
|
|
*/
|
|
|
- private boolean isNotNullObject(Class pojoClass, Object object){
|
|
|
+ private boolean isNotNullObject(Class pojoClass, Object object) {
|
|
|
try {
|
|
|
Method method = pojoClass.getMethod("isNullObject");
|
|
|
- if(method!=null){
|
|
|
+ if (method != null) {
|
|
|
Object flag = method.invoke(object);
|
|
|
- if(flag!=null && true == Boolean.parseBoolean(flag.toString())){
|
|
|
+ if (flag != null && true == Boolean.parseBoolean(flag.toString())) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -287,14 +326,15 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
|
|
|
/**
|
|
|
* 获取忽略的表头信息
|
|
|
+ *
|
|
|
* @param excelParams
|
|
|
* @param params
|
|
|
*/
|
|
|
- private void ignoreHeaderHandler(Map<String, ExcelImportEntity> excelParams,ImportParams params){
|
|
|
+ private void ignoreHeaderHandler(Map<String, ExcelImportEntity> excelParams, ImportParams params) {
|
|
|
List<String> ignoreList = new ArrayList<>();
|
|
|
- for(String key:excelParams.keySet()){
|
|
|
+ for (String key : excelParams.keySet()) {
|
|
|
String temp = excelParams.get(key).getGroupName();
|
|
|
- if(temp!=null && temp.length()>0){
|
|
|
+ if (temp != null && temp.length() > 0) {
|
|
|
ignoreList.add(temp);
|
|
|
}
|
|
|
}
|
|
|
@@ -320,10 +360,10 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
//update_begin-author:taoyan date:2020622 for:当文件行数小于代码里设置的TitleRows时headRow一直为空就会出现死循环
|
|
|
int allRowNum = sheet.getPhysicalNumberOfRows();
|
|
|
//找到首行表头,每个sheet都必须至少有一行表头
|
|
|
- while(headRow == null && headBegin < allRowNum){
|
|
|
+ while (headRow == null && headBegin < allRowNum) {
|
|
|
headRow = sheet.getRow(headBegin++);
|
|
|
}
|
|
|
- if(headRow==null){
|
|
|
+ if (headRow == null) {
|
|
|
throw new Exception("不识别该文件");
|
|
|
}
|
|
|
//update-end-author:taoyan date:2020622 for:当文件行数小于代码里设置的TitleRows时headRow一直为空就会出现死循环
|
|
|
@@ -331,7 +371,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
//设置表头行数
|
|
|
if (ExcelUtil.isMergedRegion(sheet, headRow.getRowNum(), 0)) {
|
|
|
params.setHeadRows(2);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
params.setHeadRows(1);
|
|
|
}
|
|
|
cellTitle = headRow.cellIterator();
|
|
|
@@ -353,7 +393,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
}
|
|
|
|
|
|
//多行表头
|
|
|
- for (int j = headBegin; j < headBegin + params.getHeadRows()-1; j++) {
|
|
|
+ for (int j = headBegin; j < headBegin + params.getHeadRows() - 1; j++) {
|
|
|
headRow = sheet.getRow(j);
|
|
|
cellTitle = headRow.cellIterator();
|
|
|
while (cellTitle.hasNext()) {
|
|
|
@@ -362,20 +402,20 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
if (StringUtils.isNotEmpty(value)) {
|
|
|
int columnIndex = cell.getColumnIndex();
|
|
|
//当前cell的上一行是否为合并单元格
|
|
|
- if(ExcelUtil.isMergedRegion(sheet, cell.getRowIndex()-1, columnIndex)){
|
|
|
- collectionName = ExcelUtil.getMergedRegionValue(sheet, cell.getRowIndex()-1, columnIndex);
|
|
|
- if(params.isIgnoreHeader(collectionName)){
|
|
|
+ if (ExcelUtil.isMergedRegion(sheet, cell.getRowIndex() - 1, columnIndex)) {
|
|
|
+ collectionName = ExcelUtil.getMergedRegionValue(sheet, cell.getRowIndex() - 1, columnIndex);
|
|
|
+ if (params.isIgnoreHeader(collectionName)) {
|
|
|
titlemap.put(cell.getColumnIndex(), value);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
titlemap.put(cell.getColumnIndex(), collectionName + "_" + value);
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
//update-begin-author:taoyan date:20220112 for: JT640 【online】导入 无论一对一还是一对多 如果子表只有一个字段 则子表无数据
|
|
|
// 上一行不是合并的情况下另有一种特殊的场景: 如果当前单元格和上面的单元格同一列 即子表字段只有一个 所以标题没有出现跨列
|
|
|
String prefixTitle = titlemap.get(cell.getColumnIndex());
|
|
|
- if(prefixTitle!=null && !"".equals(prefixTitle)){
|
|
|
- titlemap.put(cell.getColumnIndex(), prefixTitle + "_" +value);
|
|
|
- }else{
|
|
|
+ if (prefixTitle != null && !"".equals(prefixTitle)) {
|
|
|
+ titlemap.put(cell.getColumnIndex(), prefixTitle + "_" + value);
|
|
|
+ } else {
|
|
|
titlemap.put(cell.getColumnIndex(), value);
|
|
|
}
|
|
|
//update-end-author:taoyan date:20220112 for: JT640 【online】导入 无论一对一还是一对多 如果子表只有一个字段 则子表无数据
|
|
|
@@ -401,6 +441,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
return titlemap;
|
|
|
}
|
|
|
//update-end--Author:xuelin Date:20171205 for:TASK #2098 【excel问题】 Online 一对多导入失败--------------------
|
|
|
+
|
|
|
/**
|
|
|
* 获取这个名称对应的集合信息
|
|
|
*
|
|
|
@@ -443,18 +484,18 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
// isXSSFWorkbook=false;
|
|
|
// }
|
|
|
book = WorkbookFactory.create(inputstream);
|
|
|
- if(book instanceof XSSFWorkbook){
|
|
|
- isXSSFWorkbook=true;
|
|
|
+ if (book instanceof XSSFWorkbook) {
|
|
|
+ isXSSFWorkbook = true;
|
|
|
}
|
|
|
- LOGGER.info(" >>> poi3升级到4.0兼容改造工作, isXSSFWorkbook = " +isXSSFWorkbook);
|
|
|
+ LOGGER.info(" >>> poi3升级到4.0兼容改造工作, isXSSFWorkbook = " + isXSSFWorkbook);
|
|
|
//end-------author:liusq------date:20210129-----for:-------poi3升级到4兼容改造工作【重要敏感修改点】--------
|
|
|
|
|
|
//begin-------author:liusq------date:20210313-----for:-------多sheet导入改造点--------
|
|
|
//获取导入文本的sheet数
|
|
|
//update-begin-author:taoyan date:20211210 for:https://gitee.com/jeecg/jeecg-boot/issues/I45C32 导入空白sheet报错
|
|
|
- if(params.getSheetNum()==0){
|
|
|
+ if (params.getSheetNum() == 0) {
|
|
|
int sheetNum = book.getNumberOfSheets();
|
|
|
- if(sheetNum>0){
|
|
|
+ if (sheetNum > 0) {
|
|
|
params.setSheetNum(sheetNum);
|
|
|
}
|
|
|
}
|
|
|
@@ -488,8 +529,8 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
}
|
|
|
return new ExcelImportResult(result, verfiyFail, book);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- *
|
|
|
* @param is
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
@@ -541,7 +582,6 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param object
|
|
|
* @param picId
|
|
|
* @param excelParams
|
|
|
@@ -551,7 +591,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private void saveImage(Object object, String picId, Map<String, ExcelImportEntity> excelParams, String titleString, Map<String, PictureData> pictures, ImportParams params) throws Exception {
|
|
|
- if (pictures == null || pictures.get(picId)==null) {
|
|
|
+ if (pictures == null || pictures.get(picId) == null) {
|
|
|
return;
|
|
|
}
|
|
|
PictureData image = pictures.get(picId);
|
|
|
@@ -560,7 +600,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
fileName += "." + PoiPublicUtil.getFileExtendName(data);
|
|
|
//update-beign-author:taoyan date:20200302 for:【多任务】online 专项集中问题 LOWCOD-159
|
|
|
int saveType = excelParams.get(titleString).getSaveType();
|
|
|
- if ( saveType == 1) {
|
|
|
+ if (saveType == 1) {
|
|
|
String path = PoiPublicUtil.getWebRootPath(getSaveUrl(excelParams.get(titleString), object));
|
|
|
File savefile = new File(path);
|
|
|
if (!savefile.exists()) {
|
|
|
@@ -571,7 +611,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
fos.write(data);
|
|
|
fos.close();
|
|
|
setValues(excelParams.get(titleString), object, getSaveUrl(excelParams.get(titleString), object) + "/" + fileName);
|
|
|
- } else if(saveType==2) {
|
|
|
+ } else if (saveType == 2) {
|
|
|
setValues(excelParams.get(titleString), object, data);
|
|
|
} else {
|
|
|
ImportFileServiceI importFileService = null;
|
|
|
@@ -580,7 +620,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
} catch (Exception e) {
|
|
|
System.err.println(e.getMessage());
|
|
|
}
|
|
|
- if(importFileService!=null){
|
|
|
+ if (importFileService != null) {
|
|
|
String dbPath = importFileService.doUpload(data);
|
|
|
setValues(excelParams.get(titleString), object, dbPath);
|
|
|
}
|
|
|
@@ -597,6 +637,7 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
|
|
|
/**
|
|
|
* 根据文件流,获取上传的Excel的表头
|
|
|
+ *
|
|
|
* @param inputstream
|
|
|
* @param sheetIdx
|
|
|
* @return
|
|
|
@@ -618,10 +659,10 @@ public class DefaultExcelImportServer extends ImportBaseService {
|
|
|
//update_begin-author:taoyan date:2020622 for:当文件行数小于代码里设置的TitleRows时headRow一直为空就会出现死循环
|
|
|
int allRowNum = sheet.getPhysicalNumberOfRows();
|
|
|
//找到首行表头,每个sheet都必须至少有一行表头
|
|
|
- while(headRow == null && headBegin < allRowNum){
|
|
|
+ while (headRow == null && headBegin < allRowNum) {
|
|
|
headRow = sheet.getRow(headBegin++);
|
|
|
}
|
|
|
- if(headRow==null){
|
|
|
+ if (headRow == null) {
|
|
|
throw new Exception("不识别该文件");
|
|
|
}
|
|
|
cellTitle = headRow.cellIterator();
|