diff --git a/README.md b/README.md
index ae8010c..f61bb2e 100644
--- a/README.md
+++ b/README.md
@@ -5,11 +5,13 @@ EasyPoi Excel和 Word简易工具类
就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板
语言(熟悉的表达式语法),完成以前复杂的写法
- 作者博客:http://blog.csdn.net/qjueyue
+ 作者博客:http://blog.afterturn.cn/
作者邮箱: qrb.jueyue@gmail.com
QQ群: 364192721
[测试项目](http://git.oschina.net/jueyue/easypoi-test): http://git.oschina.net/jueyue/easypoi-test
+
+!!!2.1.6-SNAPSHOT 版本开始和之前的版本校验不兼用,使用hibernate的校验,删除了之前的注解,请注意
---------------------------
EasyPoi的主要特点
diff --git a/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/ExcelVerify.java b/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/ExcelVerify.java
deleted file mode 100644
index 86430b6..0000000
--- a/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/ExcelVerify.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * 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
- *
- * 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
- * limitations under the License.
- */
-package org.jeecgframework.poi.excel.annotation;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * Excel 导入校验
- *
- * @author JueYue
- * @date 2014年6月23日 下午10:46:26
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.FIELD)
-public @interface ExcelVerify {
- /**
- * 接口校验
- * @return
- */
- public boolean interHandler() default false;
-
- /**
- * 是电子邮件
- *
- * @return
- */
- public boolean isEmail() default false;
-
- /**
- * 是13位移动电话
- *
- * @return
- */
- public boolean isMobile() default false;
-
- /**
- * 是座机号码
- *
- * @return
- */
- public boolean isTel() default false;
-
- /**
- * 最大长度
- *
- * @return
- */
- public int maxLength() default -1;
-
- /**
- * 最小长度
- *
- * @return
- */
- public int minLength() default -1;
-
- /**
- * 不允许空
- *
- * @return
- */
- public boolean notNull() default false;
-
- /**
- * 正在表达式
- *
- * @return
- */
- public String regex() default "";
-
- /**
- * 正在表达式,错误提示信息
- *
- * @return
- */
- public String regexTip() default "数据不符合规范";
-
-}
diff --git a/easypoi-base/pom.xml b/easypoi-base/pom.xml
index 2026139..5b01e9b 100644
--- a/easypoi-base/pom.xml
+++ b/easypoi-base/pom.xml
@@ -70,6 +70,11 @@
slf4j-api
+
+ org.hibernate
+ hibernate-validator
+
+
org.jeecg
easypoi-annotation
diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/FileLoadeImpl.java b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/FileLoadeImpl.java
index cdacdcd..6c11466 100644
--- a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/FileLoadeImpl.java
+++ b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/FileLoadeImpl.java
@@ -20,6 +20,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import org.apache.poi.util.IOUtils;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,14 +59,8 @@ public class FileLoadeImpl implements IFileLoader {
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} finally {
- try {
- if (fileis != null)
- fileis.close();
- if (fileis != null)
- baos.close();
- } catch (IOException e) {
- LOGGER.error(e.getMessage(), e);
- }
+ IOUtils.closeQuietly(fileis);
+ IOUtils.closeQuietly(baos);
}
LOGGER.error(fileis + "这个路径文件没有找到,请查询");
return null;
diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java
index cc44e11..585b0b4 100644
--- a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java
+++ b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java
@@ -33,7 +33,8 @@ import com.google.common.cache.LoadingCache;
*
* @author JueYue
* @date 2014年2月10日
- * @version 1.0
+ * @date 2015年10月17日
+ * @version 1.1
*/
public final class POICacheManager {
diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java
index 71c2f68..c7b957e 100644
--- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java
+++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java
@@ -24,6 +24,7 @@ import org.jeecgframework.poi.handler.inter.IExcelVerifyHandler;
* @date 2013-9-24
* @version 1.0
*/
+@SuppressWarnings("rawtypes")
public class ImportParams extends ExcelBaseParams {
/**
* 表格标题行数,默认0
@@ -53,6 +54,10 @@ public class ImportParams extends ExcelBaseParams {
* 是否需要保存上传的Excel,默认为false
*/
private boolean needSave = false;
+ /**
+ * 是否需要校验上传的Excel,默认为false
+ */
+ private boolean needVerfiy = false;
/**
* 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是
* upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数
@@ -147,4 +152,12 @@ public class ImportParams extends ExcelBaseParams {
this.startSheetIndex = startSheetIndex;
}
+ public boolean isNeedVerfiy() {
+ return needVerfiy;
+ }
+
+ public void setNeedVerfiy(boolean needVerfiy) {
+ this.needVerfiy = needVerfiy;
+ }
+
}
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 a59bffa..53e385e 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
@@ -39,10 +39,6 @@ public class ExcelImportEntity extends ExcelBaseEntity {
* 对应exportType
*/
private String classType;
- /**
- * 校驗參數
- */
- private ExcelVerifyEntity verify;
/**
* 后缀
*/
@@ -70,10 +66,6 @@ public class ExcelImportEntity extends ExcelBaseEntity {
return saveUrl;
}
- public ExcelVerifyEntity getVerify() {
- return verify;
- }
-
public void setClassType(String classType) {
this.classType = classType;
}
@@ -94,10 +86,6 @@ public class ExcelImportEntity extends ExcelBaseEntity {
this.saveUrl = saveUrl;
}
- public void setVerify(ExcelVerifyEntity verify) {
- this.verify = verify;
- }
-
public String getSuffix() {
return suffix;
}
diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelVerifyEntity.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelVerifyEntity.java
deleted file mode 100644
index 35a5fcc..0000000
--- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelVerifyEntity.java
+++ /dev/null
@@ -1,159 +0,0 @@
-/**
- * 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
- *
- * 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
- * limitations under the License.
- */
-package org.jeecgframework.poi.excel.entity.params;
-
-/**
- * Excel 校验对象
- *
- * @author JueYue
- * @date 2014年6月29日 下午4:24:59
- */
-public class ExcelVerifyEntity {
-
- /**
- * 接口校验
- *
- * @return
- */
- private boolean interHandler;
-
- /**
- * 不允许空
- *
- * @return
- */
- private boolean notNull;
-
- /**
- * 是13位移动电话
- *
- * @return
- */
- private boolean isMobile;
- /**
- * 是座机号码
- *
- * @return
- */
- private boolean isTel;
-
- /**
- * 是电子邮件
- *
- * @return
- */
- private boolean isEmail;
-
- /**
- * 最小长度
- *
- * @return
- */
- private int minLength;
-
- /**
- * 最大长度
- *
- * @return
- */
- private int maxLength;
-
- /**
- * 正在表达式
- *
- * @return
- */
- private String regex;
- /**
- * 正在表达式,错误提示信息
- *
- * @return
- */
- private String regexTip;
-
- public int getMaxLength() {
- return maxLength;
- }
-
- public int getMinLength() {
- return minLength;
- }
-
- public String getRegex() {
- return regex;
- }
-
- public String getRegexTip() {
- return regexTip;
- }
-
- public boolean isEmail() {
- return isEmail;
- }
-
- public boolean isInterHandler() {
- return interHandler;
- }
-
- public boolean isMobile() {
- return isMobile;
- }
-
- public boolean isNotNull() {
- return notNull;
- }
-
- public boolean isTel() {
- return isTel;
- }
-
- public void setEmail(boolean isEmail) {
- this.isEmail = isEmail;
- }
-
- public void setInterHandler(boolean interHandler) {
- this.interHandler = interHandler;
- }
-
- public void setMaxLength(int maxLength) {
- this.maxLength = maxLength;
- }
-
- public void setMinLength(int minLength) {
- this.minLength = minLength;
- }
-
- public void setMobile(boolean isMobile) {
- this.isMobile = isMobile;
- }
-
- public void setNotNull(boolean notNull) {
- this.notNull = notNull;
- }
-
- public void setRegex(String regex) {
- this.regex = regex;
- }
-
- public void setRegexTip(String regexTip) {
- this.regexTip = regexTip;
- }
-
- public void setTel(boolean isTel) {
- this.isTel = isTel;
- }
-
-}
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 585c639..2029d3a 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
@@ -27,6 +27,12 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
@@ -51,9 +57,9 @@ import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult;
import org.jeecgframework.poi.excel.imports.base.ImportBaseService;
-import org.jeecgframework.poi.excel.imports.verifys.VerifyHandlerServer;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum;
+import org.jeecgframework.poi.handler.inter.IExcelModel;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.jeecgframework.poi.util.PoiReflectorUtil;
import org.slf4j.Logger;
@@ -70,9 +76,14 @@ public class ExcelImportServer extends ImportBaseService {
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class);
- private CellValueServer cellValueServer;
+ private final static Validator validator;
+
+ static {
+ ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+ validator = factory.getValidator();
+ }
- private VerifyHandlerServer verifyHandlerServer;
+ private CellValueServer cellValueServer;
private boolean verfiyFail = false;
/**
@@ -82,7 +93,6 @@ public class ExcelImportServer extends ImportBaseService {
public ExcelImportServer() {
this.cellValueServer = new CellValueServer();
- this.verifyHandlerServer = new VerifyHandlerServer();
}
/***
@@ -147,6 +157,9 @@ public class ExcelImportServer extends ImportBaseService {
case Cell.CELL_TYPE_FORMULA:
obj = cell.getCellFormula();
break;
+ default:
+ cell.setCellType(Cell.CELL_TYPE_STRING);
+ obj = cell.getStringCellValue();
}
return obj == null ? null : obj.toString().trim();
}
@@ -231,7 +244,9 @@ public class ExcelImportServer extends ImportBaseService {
for (ExcelCollectionParams param : excelCollection) {
addListContinue(object, param, row, titlemap, targetId, pictures, params);
}
- collection.add(object);
+ if (verifyingDataValidity(object, row, params, pojoClass)) {
+ collection.add(object);
+ }
} catch (ExcelImportException e) {
if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
throw new ExcelImportException(e.getType(), e);
@@ -242,6 +257,61 @@ public class ExcelImportServer extends ImportBaseService {
return collection;
}
+ /**
+ * 校验数据合法性
+ * @param object
+ * @param row
+ * @param params
+ * @param pojoClass
+ * @return
+ */
+ private boolean verifyingDataValidity(Object object, Row row, ImportParams params,
+ Class> pojoClass) {
+ boolean isAdd = true;
+ Cell cell = null;
+ if (params.isNeedVerfiy()) {
+ Set> set = validator.validate(object);
+ if (set.size() > 0) {
+ cell = row.createCell(row.getLastCellNum());
+ String errMsg = getValidateErrMsg(set);
+ cell.setCellValue(errMsg);
+ if (object instanceof IExcelModel) {
+ IExcelModel model = (IExcelModel) object;
+ model.setErrorMsg(errMsg);
+ } else {
+ isAdd = false;
+ }
+ }
+ }
+ if (params.getVerifyHanlder() != null) {
+ ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object);
+ if (!result.isSuccess()) {
+ if (cell == null)
+ cell = row.createCell(row.getLastCellNum());
+ cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue())
+ ? cell.getStringCellValue() + "," : "") + result.getMsg());
+ if (object instanceof IExcelModel) {
+ IExcelModel model = (IExcelModel) object;
+ model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg())
+ ? model.getErrorMsg() + "," : "") + result.getMsg());
+ } else {
+ isAdd = false;
+ }
+ }
+ }
+ if (cell != null)
+ cell.setCellStyle(errorCellStyle);
+ return isAdd;
+ }
+
+ private String getValidateErrMsg(Set> set) {
+ StringBuilder builder = new StringBuilder();
+ for (ConstraintViolation
+
+ org.hibernate
+ hibernate-validator
+ 5.1.3.Final
+ true
+
+
org.jeecg