Browse Source

修复了 Import 多个集合导出的bug,导入Time 的支持,多了重复名称的支持

4.1.3.A
jueyue 9 years ago
parent
commit
e5f8e9c329
9 changed files with 197 additions and 33 deletions
  1. +12
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java
  2. +12
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java
  3. +1
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java
  4. +21
    -1
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java
  5. +63
    -16
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java
  6. +33
    -13
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java
  7. +7
    -1
      easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java
  8. +2
    -2
      easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java
  9. +46
    -0
      easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/samename/ClassName.java

+ 12
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelCollectionParams.java View File

@@ -30,6 +30,10 @@ public class ExcelCollectionParams {
* 集合对应的名称 * 集合对应的名称
*/ */
private String name; private String name;
/**
* Excel 列名称
*/
private String excelName;
/** /**
* 实体对象 * 实体对象
*/ */
@@ -62,4 +66,12 @@ public class ExcelCollectionParams {
public void setType(Class<?> type) { public void setType(Class<?> type) {
this.type = type; this.type = type;
} }
public String getExcelName() {
return excelName;
}
public void setExcelName(String excelName) {
this.excelName = excelName;
}
} }

+ 12
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java View File

@@ -43,6 +43,10 @@ public class ExcelImportEntity extends ExcelBaseEntity {
* 校驗參數 * 校驗參數
*/ */
private ExcelVerifyEntity verify; private ExcelVerifyEntity verify;
/**
* 后缀
*/
private String suffix;
private List<ExcelImportEntity> list; private List<ExcelImportEntity> list;
@@ -94,4 +98,12 @@ public class ExcelImportEntity extends ExcelBaseEntity {
this.verify = verify; this.verify = verify;
} }
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
} }

+ 1
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java View File

@@ -289,6 +289,7 @@ public class ExcelExportServer extends ExcelExportBase {
titleStyle, entity); titleStyle, entity);
cellIndex++; cellIndex++;
} }
cellIndex--;
} else if (rows == 2) { } else if (rows == 2) {
createStringCell(listRow, cellIndex, "", titleStyle, entity); createStringCell(listRow, cellIndex, "", titleStyle, entity);
sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex));


+ 21
- 1
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java View File

@@ -18,6 +18,7 @@ package org.jeecgframework.poi.excel.imports;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
@@ -62,7 +63,7 @@ public class CellValueServer {
} }
Object result = null; Object result = null;
// 日期格式比较特殊,和cell格式不一致 // 日期格式比较特殊,和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()) { if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
// 日期格式 // 日期格式
result = cell.getDateCellValue(); result = cell.getDateCellValue();
@@ -70,6 +71,9 @@ public class CellValueServer {
cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellType(Cell.CELL_TYPE_STRING);
result = getDateData(entity, cell.getStringCellValue()); 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()) { } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
result = cell.getNumericCellValue(); result = cell.getNumericCellValue();
} else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) { } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
@@ -123,6 +127,7 @@ public class CellValueServer {
} }
Object result = getCellValue(xclass, cell, entity); Object result = getCellValue(xclass, cell, entity);
if (entity != null) { if (entity != null) {
result = hanlderSuffix(entity.getSuffix(), result);
result = replaceValue(entity.getReplace(), result); result = replaceValue(entity.getReplace(), result);
} }
result = hanlderValue(dataHanlder, object, result, titleString); result = hanlderValue(dataHanlder, object, result, titleString);
@@ -147,11 +152,26 @@ public class CellValueServer {
Type[] ts = setMethod.getGenericParameterTypes(); Type[] ts = setMethod.getGenericParameterTypes();
String xclass = ts[0].toString(); String xclass = ts[0].toString();
Object result = cellEntity.getValue(); Object result = cellEntity.getValue();
result = hanlderSuffix(entity.getSuffix(), result);
result = replaceValue(entity.getReplace(), result); result = replaceValue(entity.getReplace(), result);
result = hanlderValue(dataHanlder, object, result, titleString); result = hanlderValue(dataHanlder, object, result, titleString);
return getValueByType(xclass, result); 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;
}
/** /**
* 根据返回类型获取返回值 * 根据返回类型获取返回值
* *


+ 63
- 16
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java View File

@@ -144,7 +144,7 @@ public class ExcelImportServer extends ImportBaseService {
obj = cell.getNumericCellValue(); obj = cell.getNumericCellValue();
break; 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++) { for (int j = 0; j < params.getTitleRows(); j++) {
rows.next(); rows.next();
} }
Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection);
Row row = null; 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; Object object = null;
String picId; String picId;
while (rows.hasNext()) { while (rows.hasNext()) {
@@ -244,6 +230,67 @@ public class ExcelImportServer extends ImportBaseService {
return collection; 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 * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean
* *


+ 33
- 13
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java View File

@@ -24,12 +24,15 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.annotation.Excel; 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.annotation.ExcelVerify;
import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams; import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams;
@@ -56,8 +59,8 @@ public class ImportBaseService {
* @throws Exception * @throws Exception
*/ */
public void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity, 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); Excel excel = field.getAnnotation(Excel.class);
excelEntity = new ExcelImportEntity(); excelEntity = new ExcelImportEntity();
excelEntity.setType(excel.type()); excelEntity.setType(excel.type());
@@ -66,6 +69,7 @@ public class ImportBaseService {
excelEntity.setReplace(excel.replace()); excelEntity.setReplace(excel.replace());
excelEntity.setDatabaseFormat(excel.databaseFormat()); excelEntity.setDatabaseFormat(excel.databaseFormat());
excelEntity.setVerify(getImportVerify(field)); excelEntity.setVerify(getImportVerify(field));
excelEntity.setSuffix(excel.suffix());
getExcelField(targetId, field, excelEntity, excel, pojoClass); getExcelField(targetId, field, excelEntity, excel, pojoClass);
if (getMethods != null) { if (getMethods != null) {
List<Method> newMethods = new ArrayList<Method>(); List<Method> newMethods = new ArrayList<Method>();
@@ -113,9 +117,9 @@ public class ImportBaseService {
* @throws Exception * @throws Exception
*/ */
public void getAllExcelField(String targetId, Field[] fields, 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; ExcelImportEntity excelEntity = null;
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
Field field = fields[i]; Field field = fields[i];
@@ -132,6 +136,8 @@ public class ImportBaseService {
collection.setType(clz); collection.setType(clz);
getExcelFieldList(targetId, PoiPublicUtil.getClassFields(clz), clz, temp, null); getExcelFieldList(targetId, PoiPublicUtil.getClassFields(clz), clz, temp, null);
collection.setExcelParams(temp); collection.setExcelParams(temp);
collection.setExcelName(field.getAnnotation(ExcelCollection.class).name());
additionalCollectionName(collection);
excelCollection.add(collection); excelCollection.add(collection);
} else if (PoiPublicUtil.isJavaClass(field)) { } else if (PoiPublicUtil.isJavaClass(field)) {
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams); 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, 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)); excelEntity.setName(getExcelName(excel.name(), targetId));
String fieldname = field.getName(); String fieldname = field.getName();
excelEntity.setMethod(PoiPublicUtil.getMethod(fieldname, pojoClass, field.getType())); 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()); excelEntity.setFormat(excel.importFormat());
} else {
excelEntity.setFormat(excel.format());
} }
} }
public void getExcelFieldList(String targetId, Field[] fields, Class<?> pojoClass, 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; ExcelImportEntity excelEntity = null;
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
Field field = fields[i]; Field field = fields[i];
@@ -213,7 +233,7 @@ public class ImportBaseService {
} }
public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook,
Workbook book) throws Exception {
Workbook book) throws Exception {
String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass)); String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass));
File savefile = new File(path); File savefile = new File(path);
if (!savefile.exists()) { if (!savefile.exists()) {
@@ -251,7 +271,7 @@ public class ImportBaseService {
* @param object * @param object
*/ */
public void setFieldBySomeMethod(List<Method> setMethods, Object object, Object value) public void setFieldBySomeMethod(List<Method> setMethods, Object object, Object value)
throws Exception {
throws Exception {
Object t = getFieldBySomeMethod(setMethods, object); Object t = getFieldBySomeMethod(setMethods, object);
setMethods.get(setMethods.size() - 1).invoke(t, value); setMethods.get(setMethods.size() - 1).invoke(t, value);
} }


+ 7
- 1
easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java View File

@@ -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.WordImageEntity;
import org.jeecgframework.poi.word.entity.params.ExcelListEntity; import org.jeecgframework.poi.word.entity.params.ExcelListEntity;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* EASYPOI 的公共基础类 * EASYPOI 的公共基础类
@@ -60,6 +62,8 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
*/ */
public final class PoiPublicUtil { public final class PoiPublicUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(PoiPublicUtil.class);
private PoiPublicUtil() { private PoiPublicUtil() {
} }
@@ -74,7 +78,7 @@ public final class PoiPublicUtil {
Object obj = null; Object obj = null;
Method setMethod; Method setMethod;
try { try {
if(clazz.equals(Map.class)){
if (clazz.equals(Map.class)) {
return new HashMap<String, Object>(); return new HashMap<String, Object>();
} }
obj = clazz.newInstance(); obj = clazz.newInstance();
@@ -94,6 +98,7 @@ public final class PoiPublicUtil {
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException("创建对象异常"); throw new RuntimeException("创建对象异常");
} }
return obj; return obj;
@@ -271,6 +276,7 @@ public final class PoiPublicUtil {
} else if (fieldType.isPrimitive() || fieldType.getPackage() == null } else if (fieldType.isPrimitive() || fieldType.getPackage() == null
|| fieldType.getPackage().getName().equals("java.lang") || fieldType.getPackage().getName().equals("java.lang")
|| fieldType.getPackage().getName().equals("java.math") || fieldType.getPackage().getName().equals("java.math")
|| fieldType.getPackage().getName().equals("java.sql")
|| fieldType.getPackage().getName().equals("java.util")) { || fieldType.getPackage().getName().equals("java.util")) {
isBaseClass = true; isBaseClass = true;
} }


+ 2
- 2
easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/StudentEntity.java View File

@@ -28,10 +28,10 @@ public class StudentEntity implements java.io.Serializable {
@Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生") @Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生")
private int sex; private int sex;
@Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", exportFormat = "yyyy-MM-dd")
@Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
private Date birthday; private Date birthday;
@Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", exportFormat = "yyyy-MM-dd")
@Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
private java.sql.Time registrationDate; private java.sql.Time registrationDate;
/** /**
* 课程主键 * 课程主键


+ 46
- 0
easypoi-test/src/main/java/org/jeecgframework/poi/test/entity/samename/ClassName.java View File

@@ -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;
}
}

Loading…
Cancel
Save