diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java index 42bf996..10538f4 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java @@ -30,6 +30,10 @@ public class ExcelCollectionParams { * 集合对应的名称 */ private String name; + /** + * Excel 列名称 + */ + private String excelName; /** * 实体对象 */ @@ -62,4 +66,12 @@ public class ExcelCollectionParams { public void setType(Class type) { this.type = type; } + + public String getExcelName() { + return excelName; + } + + public void setExcelName(String excelName) { + this.excelName = excelName; + } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java index 737ff7d..a59bffa 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java @@ -43,6 +43,10 @@ public class ExcelImportEntity extends ExcelBaseEntity { * 校驗參數 */ private ExcelVerifyEntity verify; + /** + * 后缀 + */ + private String suffix; private List list; @@ -94,4 +98,12 @@ public class ExcelImportEntity extends ExcelBaseEntity { this.verify = verify; } + public String getSuffix() { + return suffix; + } + + public void setSuffix(String suffix) { + this.suffix = suffix; + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java index f86f737..5f6bed4 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java @@ -289,6 +289,7 @@ public class ExcelExportServer extends ExcelExportBase { titleStyle, entity); cellIndex++; } + cellIndex--; } else if (rows == 2) { createStringCell(listRow, cellIndex, "", titleStyle, entity); sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java index f47c7d4..aae3d7a 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java @@ -18,6 +18,7 @@ package org.jeecgframework.poi.excel.imports; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.math.BigDecimal; +import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; @@ -62,7 +63,7 @@ public class CellValueServer { } Object result = null; // 日期格式比较特殊,和cell格式不一致 - if ("class java.util.Date".equals(xclass)) { + if ("class java.util.Date".equals(xclass) || ("class java.sql.Time").equals(xclass)) { if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { // 日期格式 result = cell.getDateCellValue(); @@ -70,6 +71,9 @@ public class CellValueServer { cell.setCellType(Cell.CELL_TYPE_STRING); result = getDateData(entity, cell.getStringCellValue()); } + if (("class java.sql.Time").equals(xclass)) { + result = new Time(((Date)result).getTime()); + } } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { result = cell.getNumericCellValue(); } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) { @@ -123,6 +127,7 @@ public class CellValueServer { } Object result = getCellValue(xclass, cell, entity); if (entity != null) { + result = hanlderSuffix(entity.getSuffix(), result); result = replaceValue(entity.getReplace(), result); } result = hanlderValue(dataHanlder, object, result, titleString); @@ -147,11 +152,26 @@ public class CellValueServer { Type[] ts = setMethod.getGenericParameterTypes(); String xclass = ts[0].toString(); Object result = cellEntity.getValue(); + result = hanlderSuffix(entity.getSuffix(), result); result = replaceValue(entity.getReplace(), result); result = hanlderValue(dataHanlder, object, result, titleString); return getValueByType(xclass, result); } + /** + * 把后缀删除掉 + * @param result + * @param suffix + * @return + */ + private Object hanlderSuffix(String suffix, Object result) { + if (StringUtils.isNotEmpty(suffix) && result != null && result.toString().endsWith(suffix)) { + String temp = result.toString(); + return temp.substring(0, temp.length() - suffix.length()); + } + return result; + } + /** * 根据返回类型获取返回值 * diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java index b98c9c2..a98bcbb 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java @@ -144,7 +144,7 @@ public class ExcelImportServer extends ImportBaseService { obj = cell.getNumericCellValue(); break; } - return obj == null ? null : obj.toString(); + return obj == null ? null : obj.toString().trim(); } /** @@ -187,22 +187,8 @@ public class ExcelImportServer extends ImportBaseService { for (int j = 0; j < params.getTitleRows(); j++) { rows.next(); } + Map titlemap = getTitleMap(rows, params, excelCollection); Row row = null; - Iterator cellTitle; - Map titlemap = new HashMap(); - for (int j = 0; j < params.getHeadRows(); j++) { - row = rows.next(); - cellTitle = row.cellIterator(); - int i = row.getFirstCellNum(); - while (cellTitle.hasNext()) { - Cell cell = cellTitle.next(); - String value = getKeyValue(cell); - if (!StringUtils.isEmpty(value)) { - titlemap.put(i, value); - } - i = i + 1; - } - } Object object = null; String picId; while (rows.hasNext()) { @@ -244,6 +230,67 @@ public class ExcelImportServer extends ImportBaseService { return collection; } + /** + * 获取表格字段列名对应信息 + * @param rows + * @param params + * @param excelCollection + * @return + */ + private Map getTitleMap(Iterator rows, ImportParams params, + List excelCollection) { + Map titlemap = new HashMap(); + Iterator cellTitle; + String collectionName = null; + ExcelCollectionParams collectionParams = null; + Row row = null; + //还没想到好办法判断 + for (int j = 0; j < params.getHeadRows(); j++) { + row = rows.next(); + cellTitle = row.cellIterator(); + while (cellTitle.hasNext()) { + Cell cell = cellTitle.next(); + String value = getKeyValue(cell); + int i = cell.getColumnIndex(); + //用以支持重名导入 + if (StringUtils.isNotEmpty(value)) { + if (titlemap.containsKey(i)) { + collectionName = titlemap.get(i); + collectionParams = getCollectionParams(excelCollection, collectionName); + titlemap.put(i, collectionName + "_" + value); + } else if (StringUtils.isNotEmpty(collectionName) + && collectionParams.getExcelParams().containsKey( + collectionName + "_" + value)) { + titlemap.put(i, collectionName + "_" + value); + } else { + collectionName = null; + collectionParams = null; + } + if (StringUtils.isEmpty(collectionName)) { + titlemap.put(i, value); + } + } + } + } + return titlemap; + } + + /** + * 获取这个名称对应的集合信息 + * @param excelCollection + * @param collectionName + * @return + */ + private ExcelCollectionParams getCollectionParams(List excelCollection, + String collectionName) { + for (ExcelCollectionParams excelCollectionParams : excelCollection) { + if (collectionName.equals(excelCollectionParams.getExcelName())) { + return excelCollectionParams; + } + } + return null; + } + /** * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean * diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java index 3189fb6..333a9da 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java @@ -24,12 +24,15 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Workbook; import org.jeecgframework.poi.excel.annotation.Excel; +import org.jeecgframework.poi.excel.annotation.ExcelCollection; import org.jeecgframework.poi.excel.annotation.ExcelVerify; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams; @@ -56,8 +59,8 @@ public class ImportBaseService { * @throws Exception */ public void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity, - Class pojoClass, List getMethods, - Map temp) throws Exception { + Class pojoClass, List getMethods, + Map temp) throws Exception { Excel excel = field.getAnnotation(Excel.class); excelEntity = new ExcelImportEntity(); excelEntity.setType(excel.type()); @@ -66,6 +69,7 @@ public class ImportBaseService { excelEntity.setReplace(excel.replace()); excelEntity.setDatabaseFormat(excel.databaseFormat()); excelEntity.setVerify(getImportVerify(field)); + excelEntity.setSuffix(excel.suffix()); getExcelField(targetId, field, excelEntity, excel, pojoClass); if (getMethods != null) { List newMethods = new ArrayList(); @@ -113,9 +117,9 @@ public class ImportBaseService { * @throws Exception */ public void getAllExcelField(String targetId, Field[] fields, - Map excelParams, - List excelCollection, Class pojoClass, - List getMethods) throws Exception { + Map excelParams, + List excelCollection, Class pojoClass, + List getMethods) throws Exception { ExcelImportEntity excelEntity = null; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; @@ -132,6 +136,8 @@ public class ImportBaseService { collection.setType(clz); getExcelFieldList(targetId, PoiPublicUtil.getClassFields(clz), clz, temp, null); collection.setExcelParams(temp); + collection.setExcelName(field.getAnnotation(ExcelCollection.class).name()); + additionalCollectionName(collection); excelCollection.add(collection); } else if (PoiPublicUtil.isJavaClass(field)) { addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams); @@ -147,21 +153,35 @@ public class ImportBaseService { } } + /** + * 追加集合名称到前面 + * @param collection + */ + private void additionalCollectionName(ExcelCollectionParams collection) { + Set keys = new HashSet(); + keys.addAll(collection.getExcelParams().keySet()); + for (String key : keys) { + collection.getExcelParams().put(collection.getExcelName() + "_" + key, + collection.getExcelParams().get(key)); + collection.getExcelParams().remove(key); + } + } + public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity, - Excel excel, Class pojoClass) throws Exception { + Excel excel, Class pojoClass) throws Exception { excelEntity.setName(getExcelName(excel.name(), targetId)); String fieldname = field.getName(); excelEntity.setMethod(PoiPublicUtil.getMethod(fieldname, pojoClass, field.getType())); - if (StringUtils.isEmpty(excel.importFormat())) { - excelEntity.setFormat(excel.format()); - } else { + if (StringUtils.isNotEmpty(excel.importFormat())) { excelEntity.setFormat(excel.importFormat()); + } else { + excelEntity.setFormat(excel.format()); } } public void getExcelFieldList(String targetId, Field[] fields, Class pojoClass, - Map temp, List getMethods) - throws Exception { + Map temp, List getMethods) + throws Exception { ExcelImportEntity excelEntity = null; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; @@ -213,7 +233,7 @@ public class ImportBaseService { } public void saveThisExcel(ImportParams params, Class pojoClass, boolean isXSSFWorkbook, - Workbook book) throws Exception { + Workbook book) throws Exception { String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass)); File savefile = new File(path); if (!savefile.exists()) { @@ -251,7 +271,7 @@ public class ImportBaseService { * @param object */ public void setFieldBySomeMethod(List setMethods, Object object, Object value) - throws Exception { + throws Exception { Object t = getFieldBySomeMethod(setMethods, object); setMethods.get(setMethods.size() - 1).invoke(t, value); } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java index d80acb5..779c4c0 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java @@ -52,6 +52,8 @@ import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; import org.jeecgframework.poi.word.entity.WordImageEntity; import org.jeecgframework.poi.word.entity.params.ExcelListEntity; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * EASYPOI 的公共基础类 @@ -60,6 +62,8 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; */ public final class PoiPublicUtil { + private static final Logger LOGGER = LoggerFactory.getLogger(PoiPublicUtil.class); + private PoiPublicUtil() { } @@ -74,7 +78,7 @@ public final class PoiPublicUtil { Object obj = null; Method setMethod; try { - if(clazz.equals(Map.class)){ + if (clazz.equals(Map.class)) { return new HashMap(); } obj = clazz.newInstance(); @@ -94,6 +98,7 @@ public final class PoiPublicUtil { } } catch (Exception e) { + LOGGER.error(e.getMessage(), e); throw new RuntimeException("创建对象异常"); } return obj; @@ -271,6 +276,7 @@ public final class PoiPublicUtil { } else if (fieldType.isPrimitive() || fieldType.getPackage() == null || fieldType.getPackage().getName().equals("java.lang") || fieldType.getPackage().getName().equals("java.math") + || fieldType.getPackage().getName().equals("java.sql") || fieldType.getPackage().getName().equals("java.util")) { isBaseClass = true; } diff --git a/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java b/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java index eb20866..271b1ae 100644 --- a/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java +++ b/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java @@ -28,10 +28,10 @@ public class StudentEntity implements java.io.Serializable { @Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生") private int sex; - @Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", exportFormat = "yyyy-MM-dd") + @Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd") private Date birthday; - @Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", exportFormat = "yyyy-MM-dd") + @Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd") private java.sql.Time registrationDate; /** * 课程主键 diff --git a/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/samename/ClassName.java b/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/samename/ClassName.java new file mode 100644 index 0000000..d84ff39 --- /dev/null +++ b/easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/samename/ClassName.java @@ -0,0 +1,46 @@ +package org.jeecgframework.poi.test.entity.samename; + +import java.util.List; + +import org.jeecgframework.poi.excel.annotation.Excel; +import org.jeecgframework.poi.excel.annotation.ExcelCollection; +import org.jeecgframework.poi.test.entity.StudentEntity; + +public class ClassName { + + @Excel(name="班级") + private String name; + + @ExcelCollection(name="小组A") + private List arrA; + + @ExcelCollection(name="小组B") + private List arrB; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getArrA() { + return arrA; + } + + public void setArrA(List arrA) { + this.arrA = arrA; + } + + public List getArrB() { + return arrB; + } + + public void setArrB(List arrB) { + this.arrB = arrB; + } + + + +}