diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonServer.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonServer.java index 38be16a..e6ec95f 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonServer.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonServer.java @@ -84,7 +84,7 @@ public class ExportCommonServer { private Object dateFormatValue(Object value, ExcelExportEntity entity) throws Exception { Date temp = null; - if (value instanceof String) { + if (value instanceof String && StringUtils.isNoneEmpty(value.toString())) { SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat()); temp = format.parse(value.toString()); } else if (value instanceof Date) { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java index 84d401a..283b125 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java @@ -167,7 +167,7 @@ public class ExcelImportServer extends ImportBaseService { if (etarget != null) { targetId = etarget.value(); } - getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); + getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null, null); } Iterator rows = sheet.rowIterator(); for (int j = 0; j < params.getTitleRows(); j++) { @@ -415,7 +415,7 @@ public class ExcelImportServer extends ImportBaseService { for (int j = 0; j < rowList.size(); j++) { if (rowList.get(j).getRowNum() < rowList.get(j).getSheet().getLastRowNum()) { book.getSheetAt(i).shiftRows(rowList.get(j).getRowNum() + 1, rowList.get(j).getSheet().getLastRowNum(), -1); - } else if(rowList.get(j).getRowNum() == rowList.get(j).getSheet().getLastRowNum()){ + } else if (rowList.get(j).getRowNum() == rowList.get(j).getSheet().getLastRowNum()) { book.getSheetAt(i).shiftRows(rowList.get(j).getRowNum(), rowList.get(j).getSheet().getLastRowNum(), -1); } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java index 1196f48..2611537 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; @@ -39,6 +40,8 @@ import cn.afterturn.easypoi.excel.annotation.ExcelEntity; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.params.ExcelCollectionParams; import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity; +import cn.afterturn.easypoi.exception.excel.ExcelImportException; +import cn.afterturn.easypoi.exception.excel.enums.ExcelImportEnum; import cn.afterturn.easypoi.util.PoiPublicUtil; import cn.afterturn.easypoi.util.PoiReflectorUtil; @@ -62,7 +65,7 @@ public class ImportBaseService { */ public void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity, Class pojoClass, List getMethods, - Map temp) throws Exception { + Map temp, ExcelEntity excelEntityAnn) throws Exception { Excel excel = field.getAnnotation(Excel.class); excelEntity = new ExcelImportEntity(); excelEntity.setType(excel.type()); @@ -73,7 +76,7 @@ public class ImportBaseService { excelEntity.setSuffix(excel.suffix()); excelEntity.setImportField(Boolean .valueOf(PoiPublicUtil.getValueByTargetId(excel.isImportField(), targetId, "false"))); - getExcelField(targetId, field, excelEntity, excel, pojoClass); + getExcelField(targetId, field, excelEntity, excel, pojoClass, excelEntityAnn); if (getMethods != null) { List newMethods = new ArrayList(); newMethods.addAll(getMethods); @@ -97,7 +100,7 @@ public class ImportBaseService { public void getAllExcelField(String targetId, Field[] fields, Map excelParams, List excelCollection, Class pojoClass, - List getMethods) throws Exception { + List getMethods, ExcelEntity excelEntityAnn) throws Exception { ExcelImportEntity excelEntity = null; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; @@ -121,7 +124,7 @@ public class ImportBaseService { additionalCollectionName(collection); excelCollection.add(collection); } else if (PoiPublicUtil.isJavaClass(field)) { - addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams); + addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams, excelEntityAnn); } else { List newMethods = new ArrayList(); if (getMethods != null) { @@ -130,9 +133,12 @@ public class ImportBaseService { // newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); ExcelEntity excel = field.getAnnotation(ExcelEntity.class); + if (excel.show() && StringUtils.isEmpty(excel.name())) { + throw new ExcelImportException("if use ExcelEntity ,name mus has value ,data: " + ReflectionToStringBuilder.toString(excel), ExcelImportEnum.PARAMETER_ERROR); + } getAllExcelField(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(field.getType()), excelParams, excelCollection, - field.getType(), newMethods); + field.getType(), newMethods, excel); } } } @@ -152,11 +158,14 @@ public class ImportBaseService { } public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity, - Excel excel, Class pojoClass) throws Exception { + Excel excel, Class pojoClass, ExcelEntity excelEntityAnn) throws Exception { excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); if (StringUtils.isNoneEmpty(excel.groupName())) { excelEntity.setName(excel.groupName() + "_" + excelEntity.getName()); } + if(excelEntityAnn != null && excelEntityAnn.show()){ + excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName()); + } String fieldname = field.getName(); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(fieldname)); if (StringUtils.isNotEmpty(excel.importFormat())) { @@ -176,7 +185,7 @@ public class ImportBaseService { continue; } if (PoiPublicUtil.isJavaClass(field)) { - addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, temp); + addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, temp, null); } else { List newMethods = new ArrayList(); if (getMethods != null) { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java index 4106654..f2f083a 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java @@ -88,7 +88,7 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { if (etarget != null) { targetId = etarget.value(); } - getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); + getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null,null); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelImportException(e.getMessage()); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/exception/excel/enums/ExcelImportEnum.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/exception/excel/enums/ExcelImportEnum.java index bb83bcd..22b1e38 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/exception/excel/enums/ExcelImportEnum.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/exception/excel/enums/ExcelImportEnum.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Unless required by applicable law or agreed to in writing, software + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and @@ -21,10 +21,10 @@ package cn.afterturn.easypoi.exception.excel.enums; * 2014年6月19日 下午10:59:51 */ public enum ExcelImportEnum { - - IS_NOT_A_VALID_TEMPLATE ("不是合法的Excel模板") , - GET_VALUE_ERROR ("Excel 值获取失败") , - VERIFY_ERROR ("值校验失败"); + PARAMETER_ERROR("参数错误"), + IS_NOT_A_VALID_TEMPLATE("不是合法的Excel模板"), + GET_VALUE_ERROR("Excel 值获取失败"), + VERIFY_ERROR("值校验失败"); private String msg;