@@ -5,11 +5,13 @@ EasyPoi Excel和 Word简易工具类 | |||||
就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 | 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 | ||||
语言(熟悉的表达式语法),完成以前复杂的写法 | 语言(熟悉的表达式语法),完成以前复杂的写法 | ||||
作者博客:http://blog.csdn.net/qjueyue | |||||
作者博客:http://blog.afterturn.cn/ | |||||
作者邮箱: qrb.jueyue@gmail.com | 作者邮箱: qrb.jueyue@gmail.com | ||||
QQ群: 364192721 | QQ群: 364192721 | ||||
[测试项目](http://git.oschina.net/jueyue/easypoi-test): http://git.oschina.net/jueyue/easypoi-test | [测试项目](http://git.oschina.net/jueyue/easypoi-test): http://git.oschina.net/jueyue/easypoi-test | ||||
!!!2.1.6-SNAPSHOT 版本开始和之前的版本校验不兼用,使用hibernate的校验,删除了之前的注解,请注意 | |||||
--------------------------- | --------------------------- | ||||
EasyPoi的主要特点 | EasyPoi的主要特点 | ||||
@@ -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 "数据不符合规范"; | |||||
} |
@@ -70,6 +70,11 @@ | |||||
<artifactId>slf4j-api</artifactId> | <artifactId>slf4j-api</artifactId> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.hibernate</groupId> | |||||
<artifactId>hibernate-validator</artifactId> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>org.jeecg</groupId> | <groupId>org.jeecg</groupId> | ||||
<artifactId>easypoi-annotation</artifactId> | <artifactId>easypoi-annotation</artifactId> | ||||
@@ -20,6 +20,7 @@ import java.io.FileInputStream; | |||||
import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import org.apache.poi.util.IOUtils; | |||||
import org.jeecgframework.poi.util.PoiPublicUtil; | import org.jeecgframework.poi.util.PoiPublicUtil; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
@@ -58,14 +59,8 @@ public class FileLoadeImpl implements IFileLoader { | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
LOGGER.error(e.getMessage(), e); | LOGGER.error(e.getMessage(), e); | ||||
} finally { | } 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 + "这个路径文件没有找到,请查询"); | LOGGER.error(fileis + "这个路径文件没有找到,请查询"); | ||||
return null; | return null; | ||||
@@ -33,7 +33,8 @@ import com.google.common.cache.LoadingCache; | |||||
* | * | ||||
* @author JueYue | * @author JueYue | ||||
* @date 2014年2月10日 | * @date 2014年2月10日 | ||||
* @version 1.0 | |||||
* @date 2015年10月17日 | |||||
* @version 1.1 | |||||
*/ | */ | ||||
public final class POICacheManager { | public final class POICacheManager { | ||||
@@ -24,6 +24,7 @@ import org.jeecgframework.poi.handler.inter.IExcelVerifyHandler; | |||||
* @date 2013-9-24 | * @date 2013-9-24 | ||||
* @version 1.0 | * @version 1.0 | ||||
*/ | */ | ||||
@SuppressWarnings("rawtypes") | |||||
public class ImportParams extends ExcelBaseParams { | public class ImportParams extends ExcelBaseParams { | ||||
/** | /** | ||||
* 表格标题行数,默认0 | * 表格标题行数,默认0 | ||||
@@ -53,6 +54,10 @@ public class ImportParams extends ExcelBaseParams { | |||||
* 是否需要保存上传的Excel,默认为false | * 是否需要保存上传的Excel,默认为false | ||||
*/ | */ | ||||
private boolean needSave = false; | private boolean needSave = false; | ||||
/** | |||||
* 是否需要校验上传的Excel,默认为false | |||||
*/ | |||||
private boolean needVerfiy = false; | |||||
/** | /** | ||||
* 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 | * 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 | ||||
* upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 | * upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 | ||||
@@ -147,4 +152,12 @@ public class ImportParams extends ExcelBaseParams { | |||||
this.startSheetIndex = startSheetIndex; | this.startSheetIndex = startSheetIndex; | ||||
} | } | ||||
public boolean isNeedVerfiy() { | |||||
return needVerfiy; | |||||
} | |||||
public void setNeedVerfiy(boolean needVerfiy) { | |||||
this.needVerfiy = needVerfiy; | |||||
} | |||||
} | } |
@@ -39,10 +39,6 @@ public class ExcelImportEntity extends ExcelBaseEntity { | |||||
* 对应exportType | * 对应exportType | ||||
*/ | */ | ||||
private String classType; | private String classType; | ||||
/** | |||||
* 校驗參數 | |||||
*/ | |||||
private ExcelVerifyEntity verify; | |||||
/** | /** | ||||
* 后缀 | * 后缀 | ||||
*/ | */ | ||||
@@ -70,10 +66,6 @@ public class ExcelImportEntity extends ExcelBaseEntity { | |||||
return saveUrl; | return saveUrl; | ||||
} | } | ||||
public ExcelVerifyEntity getVerify() { | |||||
return verify; | |||||
} | |||||
public void setClassType(String classType) { | public void setClassType(String classType) { | ||||
this.classType = classType; | this.classType = classType; | ||||
} | } | ||||
@@ -94,10 +86,6 @@ public class ExcelImportEntity extends ExcelBaseEntity { | |||||
this.saveUrl = saveUrl; | this.saveUrl = saveUrl; | ||||
} | } | ||||
public void setVerify(ExcelVerifyEntity verify) { | |||||
this.verify = verify; | |||||
} | |||||
public String getSuffix() { | public String getSuffix() { | ||||
return suffix; | return suffix; | ||||
} | } | ||||
@@ -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; | |||||
} | |||||
} |
@@ -27,6 +27,12 @@ import java.util.HashMap; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | 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.commons.lang3.StringUtils; | ||||
import org.apache.poi.POIXMLDocument; | 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.ExcelImportResult; | ||||
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | ||||
import org.jeecgframework.poi.excel.imports.base.ImportBaseService; | 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.ExcelImportException; | ||||
import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum; | 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.PoiPublicUtil; | ||||
import org.jeecgframework.poi.util.PoiReflectorUtil; | import org.jeecgframework.poi.util.PoiReflectorUtil; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
@@ -70,9 +76,14 @@ public class ExcelImportServer extends ImportBaseService { | |||||
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); | 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; | private boolean verfiyFail = false; | ||||
/** | /** | ||||
@@ -82,7 +93,6 @@ public class ExcelImportServer extends ImportBaseService { | |||||
public ExcelImportServer() { | public ExcelImportServer() { | ||||
this.cellValueServer = new CellValueServer(); | this.cellValueServer = new CellValueServer(); | ||||
this.verifyHandlerServer = new VerifyHandlerServer(); | |||||
} | } | ||||
/*** | /*** | ||||
@@ -147,6 +157,9 @@ public class ExcelImportServer extends ImportBaseService { | |||||
case Cell.CELL_TYPE_FORMULA: | case Cell.CELL_TYPE_FORMULA: | ||||
obj = cell.getCellFormula(); | obj = cell.getCellFormula(); | ||||
break; | break; | ||||
default: | |||||
cell.setCellType(Cell.CELL_TYPE_STRING); | |||||
obj = cell.getStringCellValue(); | |||||
} | } | ||||
return obj == null ? null : obj.toString().trim(); | return obj == null ? null : obj.toString().trim(); | ||||
} | } | ||||
@@ -231,7 +244,9 @@ public class ExcelImportServer extends ImportBaseService { | |||||
for (ExcelCollectionParams param : excelCollection) { | for (ExcelCollectionParams param : excelCollection) { | ||||
addListContinue(object, param, row, titlemap, targetId, pictures, params); | addListContinue(object, param, row, titlemap, targetId, pictures, params); | ||||
} | } | ||||
collection.add(object); | |||||
if (verifyingDataValidity(object, row, params, pojoClass)) { | |||||
collection.add(object); | |||||
} | |||||
} catch (ExcelImportException e) { | } catch (ExcelImportException e) { | ||||
if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { | if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { | ||||
throw new ExcelImportException(e.getType(), e); | throw new ExcelImportException(e.getType(), e); | ||||
@@ -242,6 +257,61 @@ public class ExcelImportServer extends ImportBaseService { | |||||
return collection; | 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<ConstraintViolation<Object>> 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<ConstraintViolation<Object>> set) { | |||||
StringBuilder builder = new StringBuilder(); | |||||
for (ConstraintViolation<Object> constraintViolation : set) { | |||||
builder.append(constraintViolation.getMessage()).append(","); | |||||
} | |||||
return builder.substring(0, builder.length() - 1).toString(); | |||||
} | |||||
/** | /** | ||||
* 获取表格字段列名对应信息 | * 获取表格字段列名对应信息 | ||||
* @param rows | * @param rows | ||||
@@ -381,17 +451,7 @@ public class ExcelImportServer extends ImportBaseService { | |||||
((Map) object).put(titleString, value); | ((Map) object).put(titleString, value); | ||||
} | } | ||||
} else { | } else { | ||||
ExcelVerifyHanlderResult verifyResult = verifyHandlerServer.verifyData(object, value, | |||||
titleString, excelParams.get(titleString).getVerify(), params.getVerifyHanlder()); | |||||
if (verifyResult.isSuccess()) { | |||||
setValues(excelParams.get(titleString), object, value); | |||||
} else { | |||||
Cell errorCell = row.createCell(row.getLastCellNum()); | |||||
errorCell.setCellValue(verifyResult.getMsg()); | |||||
errorCell.setCellStyle(errorCellStyle); | |||||
verfiyFail = true; | |||||
throw new ExcelImportException(ExcelImportEnum.VERIFY_ERROR); | |||||
} | |||||
setValues(excelParams.get(titleString), object, value); | |||||
} | } | ||||
} | } | ||||
@@ -31,13 +31,12 @@ 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.apache.poi.util.IOUtils; | |||||
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.ExcelCollection; | ||||
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; | ||||
import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | ||||
import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity; | |||||
import org.jeecgframework.poi.util.PoiPublicUtil; | import org.jeecgframework.poi.util.PoiPublicUtil; | ||||
import org.jeecgframework.poi.util.PoiReflectorUtil; | import org.jeecgframework.poi.util.PoiReflectorUtil; | ||||
@@ -69,7 +68,6 @@ public class ImportBaseService { | |||||
excelEntity.setSaveType(excel.imageType()); | excelEntity.setSaveType(excel.imageType()); | ||||
excelEntity.setReplace(excel.replace()); | excelEntity.setReplace(excel.replace()); | ||||
excelEntity.setDatabaseFormat(excel.databaseFormat()); | excelEntity.setDatabaseFormat(excel.databaseFormat()); | ||||
excelEntity.setVerify(getImportVerify(field)); | |||||
excelEntity.setSuffix(excel.suffix()); | excelEntity.setSuffix(excel.suffix()); | ||||
getExcelField(targetId, field, excelEntity, excel, pojoClass); | getExcelField(targetId, field, excelEntity, excel, pojoClass); | ||||
if (getMethods != null) { | if (getMethods != null) { | ||||
@@ -82,30 +80,6 @@ public class ImportBaseService { | |||||
} | } | ||||
/** | |||||
* 获取导入校验参数 | |||||
* | |||||
* @param field | |||||
* @return | |||||
*/ | |||||
public ExcelVerifyEntity getImportVerify(Field field) { | |||||
ExcelVerify verify = field.getAnnotation(ExcelVerify.class); | |||||
if (verify != null) { | |||||
ExcelVerifyEntity entity = new ExcelVerifyEntity(); | |||||
entity.setEmail(verify.isEmail()); | |||||
entity.setInterHandler(verify.interHandler()); | |||||
entity.setMaxLength(verify.maxLength()); | |||||
entity.setMinLength(verify.minLength()); | |||||
entity.setMobile(verify.isMobile()); | |||||
entity.setNotNull(verify.notNull()); | |||||
entity.setRegex(verify.regex()); | |||||
entity.setRegexTip(verify.regexTip()); | |||||
entity.setTel(verify.isTel()); | |||||
return entity; | |||||
} | |||||
return null; | |||||
} | |||||
/** | /** | ||||
* 获取需要导出的全部字段 | * 获取需要导出的全部字段 | ||||
* | * | ||||
@@ -245,7 +219,7 @@ public class ImportBaseService { | |||||
+ Math.round(Math.random() * 100000) | + Math.round(Math.random() * 100000) | ||||
+ (isXSSFWorkbook == true ? ".xlsx" : ".xls")); | + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); | ||||
book.write(fos); | book.write(fos); | ||||
fos.close(); | |||||
IOUtils.closeQuietly(fos); | |||||
} | } | ||||
/** | /** | ||||
@@ -1,146 +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.imports.verifys; | |||||
import java.util.regex.Pattern; | |||||
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | |||||
/** | |||||
* 基础校验工具类 | |||||
* | |||||
* @author JueYue | |||||
* @date 2014年6月23日 下午11:10:12 | |||||
*/ | |||||
public class BaseVerifyHandler { | |||||
private static String NOT_NULL = "不允许为空"; | |||||
private static String IS_MOBILE = "不是手机号"; | |||||
private static String IS_TEL = "不是电话号码"; | |||||
private static String IS_EMAIL = "不是邮箱地址"; | |||||
private static String MIN_LENGHT = "小于规定长度"; | |||||
private static String MAX_LENGHT = "超过规定长度"; | |||||
private static Pattern mobilePattern = Pattern.compile("^[1][3,4,5,8,7][0-9]{9}$"); | |||||
private static Pattern telPattern = Pattern.compile("^([0][1-9]{2,3}-)?[0-9]{5,10}$"); | |||||
private static Pattern emailPattern = Pattern | |||||
.compile("^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$"); | |||||
/** | |||||
* email校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult isEmail(String name, Object val) { | |||||
if (!emailPattern.matcher(String.valueOf(val)).matches()) { | |||||
return new ExcelVerifyHanlderResult(false, name + IS_EMAIL); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 手机校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult isMobile(String name, Object val) { | |||||
if (!mobilePattern.matcher(String.valueOf(val)).matches()) { | |||||
return new ExcelVerifyHanlderResult(false, name + IS_MOBILE); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 电话校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult isTel(String name, Object val) { | |||||
if (!telPattern.matcher(String.valueOf(val)).matches()) { | |||||
return new ExcelVerifyHanlderResult(false, name + IS_TEL); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 最大长度校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult maxLength(String name, Object val, int maxLength) { | |||||
if (notNull(name, val).isSuccess() && String.valueOf(val).length() > maxLength) { | |||||
return new ExcelVerifyHanlderResult(false, name + MAX_LENGHT); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 最小长度校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @param minLength | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult minLength(String name, Object val, int minLength) { | |||||
if (notNull(name, val).isSuccess() && String.valueOf(val).length() < minLength) { | |||||
return new ExcelVerifyHanlderResult(false, name + MIN_LENGHT); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 非空校验 | |||||
* | |||||
* @param name | |||||
* @param val | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult notNull(String name, Object val) { | |||||
if (val == null || val.toString().equals("")) { | |||||
return new ExcelVerifyHanlderResult(false, name + NOT_NULL); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
/** | |||||
* 正则表达式校验 | |||||
* @param name | |||||
* @param val | |||||
* @param regex | |||||
* @param regexTip | |||||
* @return | |||||
*/ | |||||
public static ExcelVerifyHanlderResult regex(String name, Object val, String regex, | |||||
String regexTip) { | |||||
Pattern pattern = Pattern.compile(regex); | |||||
if (!pattern.matcher(String.valueOf(val)).matches()) { | |||||
return new ExcelVerifyHanlderResult(false, name + regexTip); | |||||
} | |||||
return new ExcelVerifyHanlderResult(true); | |||||
} | |||||
} |
@@ -1,88 +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.imports.verifys; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity; | |||||
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | |||||
import org.jeecgframework.poi.handler.inter.IExcelVerifyHandler; | |||||
/** | |||||
* 校验服务 | |||||
* | |||||
* @author JueYue | |||||
* @date 2014年6月29日 下午4:37:56 | |||||
*/ | |||||
public class VerifyHandlerServer { | |||||
private final static ExcelVerifyHanlderResult DEFAULT_RESULT = new ExcelVerifyHanlderResult( | |||||
true); | |||||
private void addVerifyResult(ExcelVerifyHanlderResult hanlderResult, | |||||
ExcelVerifyHanlderResult result) { | |||||
if (!hanlderResult.isSuccess()) { | |||||
result.setSuccess(false); | |||||
result.setMsg((StringUtils.isEmpty(result.getMsg()) ? "" : result.getMsg() + " , ") | |||||
+ hanlderResult.getMsg()); | |||||
} | |||||
} | |||||
/** | |||||
* 校驗數據 | |||||
* | |||||
* @param object | |||||
* @param value | |||||
* @param titleString | |||||
* @param verify | |||||
* @param excelVerifyHandler | |||||
*/ | |||||
public ExcelVerifyHanlderResult verifyData(Object object, Object value, String name, | |||||
ExcelVerifyEntity verify, | |||||
IExcelVerifyHandler excelVerifyHandler) { | |||||
if (verify == null) { | |||||
return DEFAULT_RESULT; | |||||
} | |||||
ExcelVerifyHanlderResult result = new ExcelVerifyHanlderResult(true, ""); | |||||
if (verify.isNotNull()) { | |||||
addVerifyResult(BaseVerifyHandler.notNull(name, value), result); | |||||
} | |||||
if (verify.isEmail()) { | |||||
addVerifyResult(BaseVerifyHandler.isEmail(name, value), result); | |||||
} | |||||
if (verify.isMobile()) { | |||||
addVerifyResult(BaseVerifyHandler.isMobile(name, value), result); | |||||
} | |||||
if (verify.isTel()) { | |||||
addVerifyResult(BaseVerifyHandler.isTel(name, value), result); | |||||
} | |||||
if (verify.getMaxLength() != -1) { | |||||
addVerifyResult(BaseVerifyHandler.maxLength(name, value, verify.getMaxLength()), result); | |||||
} | |||||
if (verify.getMinLength() != -1) { | |||||
addVerifyResult(BaseVerifyHandler.minLength(name, value, verify.getMinLength()), result); | |||||
} | |||||
if (StringUtils.isNotEmpty(verify.getRegex())) { | |||||
addVerifyResult( | |||||
BaseVerifyHandler.regex(name, value, verify.getRegex(), verify.getRegexTip()), | |||||
result); | |||||
} | |||||
if (verify.isInterHandler()) { | |||||
addVerifyResult(excelVerifyHandler.verifyHandler(object, name, value), result); | |||||
} | |||||
return result; | |||||
} | |||||
} |
@@ -27,14 +27,14 @@ import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | |||||
* @author JueYue | * @author JueYue | ||||
* @date 2014年6月20日 上午12:11:52 | * @date 2014年6月20日 上午12:11:52 | ||||
*/ | */ | ||||
public abstract class ExcelDataHandlerDefaultImpl implements IExcelDataHandler { | |||||
public abstract class ExcelDataHandlerDefaultImpl<T> implements IExcelDataHandler<T> { | |||||
/** | /** | ||||
* 需要处理的字段 | * 需要处理的字段 | ||||
*/ | */ | ||||
private String[] needHandlerFields; | private String[] needHandlerFields; | ||||
@Override | @Override | ||||
public Object exportHandler(Object obj, String name, Object value) { | |||||
public Object exportHandler(T obj, String name, Object value) { | |||||
return value; | return value; | ||||
} | } | ||||
@@ -44,7 +44,7 @@ public abstract class ExcelDataHandlerDefaultImpl implements IExcelDataHandler { | |||||
} | } | ||||
@Override | @Override | ||||
public Object importHandler(Object obj, String name, Object value) { | |||||
public Object importHandler(T obj, String name, Object value) { | |||||
return value; | return value; | ||||
} | } | ||||
@@ -59,7 +59,7 @@ public abstract class ExcelDataHandlerDefaultImpl implements IExcelDataHandler { | |||||
} | } | ||||
@Override | @Override | ||||
public Hyperlink getHyperlink(CreationHelper creationHelper, Object obj, String name, Object value) { | |||||
public Hyperlink getHyperlink(CreationHelper creationHelper, T obj, String name, Object value) { | |||||
return null; | return null; | ||||
} | } | ||||
@@ -26,7 +26,7 @@ import org.apache.poi.ss.usermodel.Hyperlink; | |||||
* @author JueYue | * @author JueYue | ||||
* @date 2014年6月19日 下午11:59:45 | * @date 2014年6月19日 下午11:59:45 | ||||
*/ | */ | ||||
public interface IExcelDataHandler { | |||||
public interface IExcelDataHandler<T> { | |||||
/** | /** | ||||
* 导出处理方法 | * 导出处理方法 | ||||
@@ -39,7 +39,7 @@ public interface IExcelDataHandler { | |||||
* 当前值 | * 当前值 | ||||
* @return | * @return | ||||
*/ | */ | ||||
public Object exportHandler(Object obj, String name, Object value); | |||||
public Object exportHandler(T obj, String name, Object value); | |||||
/** | /** | ||||
* 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | ||||
@@ -59,7 +59,7 @@ public interface IExcelDataHandler { | |||||
* 当前值 | * 当前值 | ||||
* @return | * @return | ||||
*/ | */ | ||||
public Object importHandler(Object obj, String name, Object value); | |||||
public Object importHandler(T obj, String name, Object value); | |||||
/** | /** | ||||
* 设置需要处理的属性列表 | * 设置需要处理的属性列表 | ||||
@@ -83,6 +83,6 @@ public interface IExcelDataHandler { | |||||
* @param value | * @param value | ||||
* @return | * @return | ||||
*/ | */ | ||||
public Hyperlink getHyperlink(CreationHelper creationHelper, Object obj, String name, Object value); | |||||
public Hyperlink getHyperlink(CreationHelper creationHelper, T obj, String name, Object value); | |||||
} | } |
@@ -0,0 +1,35 @@ | |||||
/** | |||||
* 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.handler.inter; | |||||
/** | |||||
* Excel标记类 | |||||
* @author JueYue | |||||
* @date 2015年10月31日 下午9:31:47 | |||||
*/ | |||||
public interface IExcelModel { | |||||
/** | |||||
* 获取错误数据 | |||||
* @return | |||||
*/ | |||||
public String getErrorMsg(); | |||||
/** | |||||
* 设置错误信息 | |||||
* @param errorMsg | |||||
*/ | |||||
public void setErrorMsg(String errorMsg); | |||||
} |
@@ -23,33 +23,15 @@ import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | |||||
* @author JueYue | * @author JueYue | ||||
* @date 2014年6月23日 下午11:08:21 | * @date 2014年6月23日 下午11:08:21 | ||||
*/ | */ | ||||
public interface IExcelVerifyHandler { | |||||
/** | |||||
* 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | |||||
* | |||||
* @return | |||||
*/ | |||||
public String[] getNeedVerifyFields(); | |||||
/** | |||||
* 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | |||||
* | |||||
* @return | |||||
*/ | |||||
public void setNeedVerifyFields(String[] arr); | |||||
public interface IExcelVerifyHandler<T> { | |||||
/** | /** | ||||
* 导出处理方法 | * 导出处理方法 | ||||
* | * | ||||
* @param obj | * @param obj | ||||
* 当前对象 | * 当前对象 | ||||
* @param name | |||||
* 当前字段名称 | |||||
* @param value | |||||
* 当前值 | |||||
* @return | * @return | ||||
*/ | */ | ||||
public ExcelVerifyHanlderResult verifyHandler(Object obj, String name, Object value); | |||||
public ExcelVerifyHanlderResult verifyHandler(T obj); | |||||
} | } |
@@ -152,6 +152,13 @@ | |||||
<version>5.2.0</version> | <version>5.2.0</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.hibernate</groupId> | |||||
<artifactId>hibernate-validator</artifactId> | |||||
<version>5.1.3.Final</version> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<!-- 模块版本 --> | <!-- 模块版本 --> | ||||
<dependency> | <dependency> | ||||
<groupId>org.jeecg</groupId> | <groupId>org.jeecg</groupId> | ||||