| @@ -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; | |||
| } | |||
| } | |||
| @@ -43,6 +43,10 @@ public class ExcelImportEntity extends ExcelBaseEntity { | |||
| * 校驗參數 | |||
| */ | |||
| private ExcelVerifyEntity verify; | |||
| /** | |||
| * 后缀 | |||
| */ | |||
| private String suffix; | |||
| private List<ExcelImportEntity> 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; | |||
| } | |||
| } | |||
| @@ -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)); | |||
| @@ -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; | |||
| } | |||
| /** | |||
| * 根据返回类型获取返回值 | |||
| * | |||
| @@ -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<Integer, String> titlemap = getTitleMap(rows, params, excelCollection); | |||
| Row row = null; | |||
| Iterator<Cell> cellTitle; | |||
| Map<Integer, String> titlemap = new HashMap<Integer, String>(); | |||
| 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<Integer, String> getTitleMap(Iterator<Row> rows, ImportParams params, | |||
| List<ExcelCollectionParams> excelCollection) { | |||
| Map<Integer, String> titlemap = new HashMap<Integer, String>(); | |||
| Iterator<Cell> 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<ExcelCollectionParams> excelCollection, | |||
| String collectionName) { | |||
| for (ExcelCollectionParams excelCollectionParams : excelCollection) { | |||
| if (collectionName.equals(excelCollectionParams.getExcelName())) { | |||
| return excelCollectionParams; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| /** | |||
| * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean | |||
| * | |||
| @@ -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<Method> getMethods, | |||
| Map<String, ExcelImportEntity> temp) throws Exception { | |||
| Class<?> pojoClass, List<Method> getMethods, | |||
| Map<String, ExcelImportEntity> 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<Method> newMethods = new ArrayList<Method>(); | |||
| @@ -113,9 +117,9 @@ public class ImportBaseService { | |||
| * @throws Exception | |||
| */ | |||
| public void getAllExcelField(String targetId, Field[] fields, | |||
| Map<String, ExcelImportEntity> excelParams, | |||
| List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, | |||
| List<Method> getMethods) throws Exception { | |||
| Map<String, ExcelImportEntity> excelParams, | |||
| List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, | |||
| List<Method> 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<String> keys = new HashSet<String>(); | |||
| 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<String, ExcelImportEntity> temp, List<Method> getMethods) | |||
| throws Exception { | |||
| Map<String, ExcelImportEntity> temp, List<Method> 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<Method> setMethods, Object object, Object value) | |||
| throws Exception { | |||
| throws Exception { | |||
| Object t = getFieldBySomeMethod(setMethods, object); | |||
| setMethods.get(setMethods.size() - 1).invoke(t, value); | |||
| } | |||
| @@ -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<String, Object>(); | |||
| } | |||
| 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; | |||
| } | |||
| @@ -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; | |||
| /** | |||
| * 课程主键 | |||
| @@ -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<StudentEntity> arrA; | |||
| @ExcelCollection(name="小组B") | |||
| private List<StudentEntity> arrB; | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| public void setName(String name) { | |||
| this.name = name; | |||
| } | |||
| public List<StudentEntity> getArrA() { | |||
| return arrA; | |||
| } | |||
| public void setArrA(List<StudentEntity> arrA) { | |||
| this.arrA = arrA; | |||
| } | |||
| public List<StudentEntity> getArrB() { | |||
| return arrB; | |||
| } | |||
| public void setArrB(List<StudentEntity> arrB) { | |||
| this.arrB = arrB; | |||
| } | |||
| } | |||