Excel导入工具@ExcelField(attrName="")属性的支持

This commit is contained in:
thinkgem
2018-06-04 23:10:25 +08:00
parent 98ab05793b
commit 226d20d759
9 changed files with 86 additions and 47 deletions

View File

@@ -69,7 +69,7 @@ public class ReflectUtils {
}
}
}
/**
* 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
*/
@@ -141,6 +141,9 @@ public class ReflectUtils {
if (args[i] != null && !args[i].getClass().equals(cs[i])){
if (cs[i] == String.class){
args[i] = ObjectUtils.toString(args[i]);
if(StringUtils.endsWith((String)args[i], ".0")){
args[i] = StringUtils.substringBefore((String)args[i], ".0");
}
}else if (cs[i] == Integer.class){
args[i] = ObjectUtils.toInteger(args[i]);
}else if (cs[i] == Long.class){

View File

@@ -240,7 +240,7 @@ public class ExcelExport {
// 创建工作表
this.createSheet(sheetName, title, headerList, headerWidthList);
}
/**
* 添加到 annotationList
*/
@@ -266,7 +266,7 @@ public class ExcelExport {
}
}
}
/**
* 创建工作表
* @param sheetName 指定Sheet名称

View File

@@ -416,34 +416,40 @@ public class ExcelImport {
}
//log.debug("Import value type: ["+i+","+column+"] " + valType);
try {
if (val != null){
if (valType == String.class){
String s = String.valueOf(val.toString());
if(StringUtils.endsWith(s, ".0")){
val = StringUtils.substringBefore(s, ".0");
if (StringUtils.isNotBlank(ef.attrName())){
if (ef.fieldType() != Class.class){
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val);
}
}else{
if (val != null){
if (valType == String.class){
String s = String.valueOf(val.toString());
if(StringUtils.endsWith(s, ".0")){
val = StringUtils.substringBefore(s, ".0");
}else{
val = String.valueOf(val.toString());
}
}else if (valType == Integer.class){
val = Double.valueOf(val.toString()).intValue();
}else if (valType == Long.class){
val = Double.valueOf(val.toString()).longValue();
}else if (valType == Double.class){
val = Double.valueOf(val.toString());
}else if (valType == Float.class){
val = Float.valueOf(val.toString());
}else if (valType == Date.class){
if (val instanceof String){
val = DateUtils.parseDate(val);
}else if (val instanceof Double){
val = DateUtil.getJavaDate((Double)val); // POI Excel 日期格式转换
}
}else{
val = String.valueOf(val.toString());
}
}else if (valType == Integer.class){
val = Double.valueOf(val.toString()).intValue();
}else if (valType == Long.class){
val = Double.valueOf(val.toString()).longValue();
}else if (valType == Double.class){
val = Double.valueOf(val.toString());
}else if (valType == Float.class){
val = Float.valueOf(val.toString());
}else if (valType == Date.class){
if (val instanceof String){
val = DateUtils.parseDate(val);
}else if (val instanceof Double){
val = DateUtil.getJavaDate((Double)val); // POI Excel 日期格式转换
}
}else{
if (ef.fieldType() != Class.class){
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
}else{
val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
if (ef.fieldType() != Class.class){
val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
}else{
val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
}
}
}
}
@@ -454,14 +460,18 @@ public class ExcelImport {
exceptionCallback.execute(ex, i, column);
}
// set entity value
if (os[1] instanceof Field){
ReflectUtils.invokeSetter(e, ((Field)os[1]).getName(), val);
}else if (os[1] instanceof Method){
String mthodName = ((Method)os[1]).getName();
if ("get".equals(mthodName.substring(0, 3))){
mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
if (StringUtils.isNotBlank(ef.attrName())){
ReflectUtils.invokeSetter(e, ef.attrName(), val);
}else{
if (os[1] instanceof Field){
ReflectUtils.invokeSetter(e, ((Field)os[1]).getName(), val);
}else if (os[1] instanceof Method){
String mthodName = ((Method)os[1]).getName();
if ("get".equals(mthodName.substring(0, 3))){
mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
}
ReflectUtils.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
}
ReflectUtils.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
}
}
sb.append(val+", ");

View File

@@ -183,7 +183,7 @@ html #layuicss-skinlayercss{display: none; position: absolute; width: 1989px;}
/* ThinkGem */
.layui-layer-title {font-weight:bold;font-size:15px;background:#fff;height:45px;line-height:45px;}
.layui-layer-page .layui-layer-content {overflow-x:hidden;}
.layui-layer-page .layui-layer-content .licFile{padding-top:6px;}
.layui-layer-page .layui-layer-content .form-file{padding-top:4px;}
.layui-layer-page .layui-layer-btn, .layui-layer-iframe .layui-layer-btn {
padding-top:3px;padding-bottom:10px;}
.layui-layer-btn .layui-layer-btn0{border-color:#367fa9;background-color: #367fa9;}

View File

@@ -3,6 +3,8 @@
*/
package com.jeesite.common.utils.excel.fieldtype;
import java.util.List;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.utils.AreaUtils;
@@ -15,11 +17,18 @@ import com.jeesite.modules.sys.utils.AreaUtils;
*/
public class AreaType {
private static ThreadLocal<List<Area>> cache = new ThreadLocal<>();
/**
* 获取对象值(导入)
*/
public static Object getValue(String val) {
for (Area e : AreaUtils.getAreaAllList()){
List<Area> cacheList = cache.get();
if (cacheList == null){
cacheList = AreaUtils.getAreaAllList();
cache.set(cacheList);
}
for (Area e : cacheList){
if (StringUtils.trimToEmpty(val).equals(e.getAreaName())){
return e;
}

View File

@@ -3,6 +3,8 @@
*/
package com.jeesite.common.utils.excel.fieldtype;
import java.util.List;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.utils.EmpUtils;
@@ -15,11 +17,18 @@ import com.jeesite.modules.sys.utils.EmpUtils;
*/
public class CompanyType {
private static ThreadLocal<List<Company>> cache = new ThreadLocal<>();
/**
* 获取对象值(导入)
*/
public static Object getValue(String val) {
for (Company e : EmpUtils.getCompanyAllList()){
List<Company> cacheList = cache.get();
if (cacheList == null){
cacheList = EmpUtils.getCompanyAllList();
cache.set(cacheList);
}
for (Company e : cacheList){
if (StringUtils.trimToEmpty(val).equals(e.getCompanyName())){
return e;
}

View File

@@ -3,6 +3,8 @@
*/
package com.jeesite.common.utils.excel.fieldtype;
import java.util.List;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.EmpUtils;
@@ -15,11 +17,18 @@ import com.jeesite.modules.sys.utils.EmpUtils;
*/
public class OfficeType {
private static ThreadLocal<List<Office>> cache = new ThreadLocal<>();
/**
* 获取对象值(导入)
*/
public static Object getValue(String val) {
for (Office e : EmpUtils.getOfficeAllList()){
List<Office> cacheList = cache.get();
if (cacheList == null){
cacheList = EmpUtils.getOfficeAllList();
cache.set(cacheList);
}
for (Office e : cacheList){
if (StringUtils.trimToEmpty(val).equals(e.getOfficeName())){
return e;
}

View File

@@ -27,13 +27,13 @@ public class PostListType {
*/
public static Object getValue(String val) {
List<Post> postList = ListUtils.newArrayList();
List<Post> allPostList = cache.get();
if (allPostList == null){
allPostList = postService.findList(new Post());
cache.set(allPostList); // 不知道会不会引起内存泄露,先这样用着
List<Post> cacheList = cache.get();
if (cacheList == null){
cacheList = postService.findList(new Post());
cache.set(cacheList);
}
for (String s : StringUtils.split(val, ",")) {
for (Post e : allPostList) {
for (Post e : cacheList) {
if (StringUtils.trimToEmpty(s).equals(e.getPostName())) {
postList.add(e);
}

View File

@@ -359,7 +359,6 @@ public class InitCoreData extends BaseInitDataTests {
else if("save".equals(action)){
EmpUser entity = (EmpUser)params[1];
entity.setIsNewRecord(true);
entity.setPassword(UserService.encryptPassword(entity.getPassword()));
empUserService.save(entity);
// 设置当前为管理员,否则无法保存用户角色关系
entity.setCurrentUser(new User(User.SUPER_ADMIN_CODE));