master
wangbing 5 years ago
parent 43fe35cf1f
commit 94f0b157f5

@ -1,28 +1,21 @@
package com.wb.excel.api;
import com.wb.excel.api.entity.DataVerifyResult;
import com.wb.excel.api.enumeration.Status;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* .
* Created on 2014/9/23.
*
* @author
* @since 0.1.0
*/
public class WCell implements Serializable {
/**
*
*/
private Status status;
/**
*
*/
private String value;
private List<DataVerifyResult> verifyResultList = new ArrayList<>();
/**
* ..
*/
@ -56,4 +49,12 @@ public class WCell implements Serializable {
public void setValue(String value) {
this.value = value;
}
public void addVerifyResult(DataVerifyResult verifyResult) {
verifyResultList.add(verifyResult);
}
public boolean hasVerifyResult(DataVerifyResult verifyResult) {
return verifyResultList.size() > 0;
}
}

@ -8,8 +8,8 @@ import com.wb.excel.api.exception.IllegalParameterException;
import com.wb.excel.api.exception.TemplateNotMatchException;
import com.wb.excel.api.style.*;
import com.wb.excel.api.util.ClassUtil;
import com.wb.excel.api.util.ExcelUtil;
import com.wb.excel.api.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
@ -19,6 +19,7 @@ import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@ -241,7 +242,7 @@ public class WSheet<T> implements Serializable, Cloneable {
for (int i = 0; i < list.size(); i++) {
WColumn wColumn = list.get(i);
Cell cell = headRow.getCell(i);
String headValue = ExcelUtil.getValue(cell);
String headValue = getValue(cell);
headValue = headValue.replace("*", "");
headValue = headValue.replace(" ", "");
@ -269,7 +270,7 @@ public class WSheet<T> implements Serializable, Cloneable {
String value = "";
if (null != excelCell) {
value = ExcelUtil.getValue(excelCell);
value = getValue(excelCell);
}
value = value.trim();
@ -633,4 +634,28 @@ public class WSheet<T> implements Serializable, Cloneable {
}
return workbook;
}
/**
*
*
* @param cell
* @return
*/
public static String getValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date value = cell.getDateCellValue();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
} else {
return String.valueOf(cell.getNumericCellValue());
}
default:
return cell.getStringCellValue();
}
}
}

@ -20,12 +20,12 @@ public @interface ColumnName {
*
* @return
*/
public String orderNum() default "0";
String orderNum() default "0";
/**
*
*
* @return
*/
public String dateFormat() default "yyyy-MM-dd";
String dateFormat() default "yyyy-MM-dd";
}

@ -1,39 +0,0 @@
package com.wb.excel.api.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
/**
* ***************************************************************
* <p/>
* <pre>
* Copyright (c) 2014
* ColumnDescription:
* ***************************************************************
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelCollection {
/**
*
*
* @return
*/
public String value();
/**
*
*
* @return
*/
public String orderNum() default "0";
/**
* arrayList
*/
public Class<?> type() default ArrayList.class;
}

@ -1,61 +0,0 @@
package com.wb.excel.api.entity;
import java.util.Map;
/**
* ***************************************************************
* <p/>
* <pre>
* Copyright (c) 2014
* ColumnDescription:WExcel
* ***************************************************************
* </pre>
*/
public class ExcelCollectionEntity {
/**
*
*/
private String name;
/**
* WExcel
*/
private String excelName;
/**
*
*/
private Class<?> type;
private Map<String, ExcelImportEntity> excelParams;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExcelName() {
return excelName;
}
public void setExcelName(String excelName) {
this.excelName = excelName;
}
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
public Map<String, ExcelImportEntity> getExcelParams() {
return excelParams;
}
public void setExcelParams(Map<String, ExcelImportEntity> excelParams) {
this.excelParams = excelParams;
}
}

@ -1,37 +0,0 @@
package com.wb.excel.api.entity;
/**
* ***************************************************************
* <p/>
* <pre>
* Copyright (c) 2014
* ColumnDescription:
* ***************************************************************
* </pre>
*/
public class ExcelImportEntity extends ExcelEntity {
/**
*
*/
private ExcelVerifyEntity verify;
/**
*
*/
// private String verifyFieldName ;
public ExcelVerifyEntity getVerify() {
return verify;
}
public void setVerify(ExcelVerifyEntity verify) {
this.verify = verify;
}
// public String getVerifyFieldName() {
// return verifyFieldName;
// }
//
// public void setVerifyFieldName(String verifyFieldName) {
// this.verifyFieldName = verifyFieldName;
// }
}

@ -108,7 +108,7 @@ public class ExcelImportResult<T> {
for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
row.createCell(i + 1).setCellValue(temptitle.get(i));
}
Boolean result = (dataVerifyResult == null || dataVerifyResult.isSuccess()) ? true : false;
Boolean result = dataVerifyResult == null || dataVerifyResult.isSuccess();
Cell statusCell = row.createCell(0);
if (!result) {
statusCell.setCellStyle(errorCellStyle.getStyle());

@ -1,141 +0,0 @@
package com.wb.excel.api.entity;
/**
* WExcel
*/
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;
}
}

@ -16,7 +16,7 @@ public interface EnumSupport {
* @param str
* @return true || false / ||
*/
public Boolean check(String str);
Boolean check(String str);
/**
* ValueKey<br/>
@ -25,7 +25,7 @@ public interface EnumSupport {
* @param value
* @return Key
*/
public Object getKey(String value);
Object getKey(String value);
/**
* KeyValue<br/>
@ -34,5 +34,5 @@ public interface EnumSupport {
* @param key
* @return Value
*/
public String getValue(Object key);
String getValue(Object key);
}

@ -29,6 +29,6 @@ public interface IExcelVerifyHandler {
* @param value
* @return
*/
public DataVerifyResult verifyHandler(Object obj, String name, Object value);
DataVerifyResult verifyHandler(Object obj, String name, Object value);
}

@ -1,8 +1,6 @@
package com.wb.excel.api.util;
import com.wb.excel.api.annotation.ExcelCollection;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
@ -93,11 +91,11 @@ public class ClassUtil {
getMethodName.append(name.substring(1));
Method method = null;
try {
method = pojoClass.getMethod(getMethodName.toString(), new Class[]{});
method = pojoClass.getMethod(getMethodName.toString());
} catch (Exception e) {
method = pojoClass.getMethod(
getMethodName.toString().replace("get", "is"),
new Class[]{});
getMethodName.toString().replace("get", "is")
);
}
return method;
}
@ -115,7 +113,7 @@ public class ClassUtil {
StringBuffer getMethodName = new StringBuffer("set");
getMethodName.append(name.substring(0, 1).toUpperCase());
getMethodName.append(name.substring(1));
return pojoClass.getMethod(getMethodName.toString(), new Class[]{type});
return pojoClass.getMethod(getMethodName.toString(), type);
}
/**
@ -172,31 +170,4 @@ public class ClassUtil {
}
return fieldList;
}
public static Object createObject(Class<?> clazz) {
Object obj = null;
Method setMethod;
try {
if (clazz.equals(Map.class)) {
return new HashMap<String, Object>();
}
obj = clazz.newInstance();
Field[] fields = getClassFields(clazz);
for (Field field : fields) {
if (isCollection(field.getType())) {
ExcelCollection collection = field.getAnnotation(ExcelCollection.class);
setMethod = getMethod(field.getName(), clazz, field.getType());
setMethod.invoke(obj, collection.type().newInstance());
} else if (!isJavaClass(field)) {
setMethod = getMethod(field.getName(), clazz, field.getType());
setMethod.invoke(obj, createObject(field.getType()));
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("创建对象异常");
}
return obj;
}
}

@ -1,40 +0,0 @@
package com.wb.excel.api.util;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Excel.
* Created on 2014/9/1.
*
* @author
* @since 0.1.0
*/
public class ExcelUtil {
/**
*
*
* @param cell
* @return
*/
public static String getValue(Cell cell) {
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && !HSSFDateUtil.isCellDateFormatted(cell)) {
// 返回数值类型的值
return String.valueOf(cell.getNumericCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) {
// 返回数值类型的值
Date value = cell.getDateCellValue();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
} else {
// 返回字符串类型的值
return String.valueOf(cell.getStringCellValue());
}
}
}

@ -1,237 +0,0 @@
package com.wb.excel.api.util;
import javax.validation.Validator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* Created by DEV001 on 2014/8/1.
*/
public class ValidationUtil {
private static Validator validator;
/**
* ID
*
* @param id ID
* @return true, ID
*/
public static Boolean checkId(Long id) {
return !(null == id || id < 1);
}
public static Boolean checkDate(String value) {
try {
Double.valueOf(value);
} catch (Exception e) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
sdf.parse(value);
return true;
} catch (ParseException e1) {
e1.printStackTrace();
}
return false;
}
return true;
}
public static Boolean checkDatetime(String value) {
try {
Double.valueOf(value);
} catch (Exception e) {
List<String> sdfList = new ArrayList<>();
sdfList.add("yyyy-MM-dd HH:mm:ss");
sdfList.add("yyyy-MM-dd hh:mm:ss");
sdfList.add("yyyy/MM/dd HH:mm:ss");
sdfList.add("yyyy/MM/dd hh:mm:ss");
for (int i = 0; i < sdfList.size(); i++) {
String sdfValue = sdfList.get(i);
SimpleDateFormat sdf = new SimpleDateFormat(sdfValue);
try {
sdf.parse(value);
return true;
} catch (ParseException ignored) {
}
}
return false;
}
return true;
}
public static Boolean checkDouble(String value) {
try {
Double.parseDouble(value);
} catch (Exception e) {
return false;
}
return true;
}
/**
*
*
* @param value
* @return true || false
*/
public static Boolean checkInteger(String value) {
try {
Double d = Double.parseDouble(value);
int i = d.intValue();
return d == i;
} catch (Exception e) {
return false;
}
}
/**
*
*
* @param value
* @return true || false
*/
public static boolean checkLong(String value) {
try {
Double d = Double.parseDouble(value);
long l = d.longValue();
return d == l;
} catch (Exception e) {
return false;
}
}
/**
* ID
*
* @param ids ID
* @return true, ID
*/
public static Boolean checkIdList(List<Long> ids) {
return !(null == ids || ids.size() == 0);
}
/**
* truefalse<br/>
* <b></b>
*
* @param str
* @return true,false,
*/
public static Boolean checkString(String str) {
return !(null == str || 0 == str.trim().length());
}
/**
* <br><b></b>
*
* @param str
* @param minLength
* @param maxLength
* @return 250"aa"true"a"false
*/
public static Boolean checkStringLength(String str, int minLength, int maxLength) {
if (null == str) {
return false;
} else {
if (str.length() >= minLength && str.length() <= maxLength) {
return true;
}
}
return false;
}
/**
* <br/>
*
* @param email
* @return true, false<br/>
* <br/>
* 0123-abcd_ABCD@0123-abcd_ABCD.00aaBB--__.cc <b>true</b><br/>
* a@a.a <b>true</b><br/>
* tony@sina@qq.com <b>false</b>
*/
public static Boolean checkEmail(String email) {
//验证邮箱地址的正则表达式
Pattern p;
p = Pattern.compile(
"^([a-zA-Z0-9]+[_|_|-|-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|-|-|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,3}$");
Matcher m = p.matcher(email);
return m.matches();
}
/**
* <br/>
*
* @param filePath
* @return true, false<br/>
* <br/>
* C:\b.txt <b>true</b><br/>
* C:\abc\a.txt <b>true</b><br/>
* C:\windows\sys<\mfc.dll <b>false</b>
*/
public static Boolean checkFilePath(String filePath) {
//验证文件路径格式的正则表达式
Pattern p = Pattern.compile("^(?<path>(?:[a-zA-Z]:)?\\\\(?:[^\\\\\\?\\/\\*\\|<>:\"]+\\\\)+)(?<filename>(?<name>[^\\\\\\?\\/\\*\\|<>:\"]+?)\\.(?<ext>[^.\\\\\\?\\/\\*\\|<>:\"]+))$");
Matcher m = p.matcher(filePath);
return m.matches();
}
/**
*
*
* @param phone
* @return true, false<br>
* <br>
* 13345661234 <b>true</b> <br>
* 16812341234 <b>false</b> <br>
* 0512-123456 <b>false</b> <br>
* 0512-2345678 <b>true</b> <br>
* 12345678 <b>false</b> <br>
* 22345678 <b>true</b>
*/
public static Boolean checkPhone(String phone) {
List<Pattern> list = new LinkedList<>();
list.add(Pattern.compile("^[1][3,4,5,8][0-9]{9}$")); //验证手机号
list.add(Pattern.compile("^[0][1-9]{1,2}[0-9]-[2-9][0-9]{6,7}$")); //验证带区号固定电话,区号后加“-”
list.add(Pattern.compile("^[2-9][0-9]{6,7}$")); //验证不带区号的固定电话
list.add(Pattern.compile("([0][1-9]{1,2}[0-9])[2-9][0-9]{6,7}$"));
Matcher matcher;
Boolean result = false;
for (Pattern pattern : list) {
matcher = pattern.matcher(phone); //遍历匹配
result |= matcher.matches(); //对所有的匹配结果做或运算。
}
return result;
}
/**
* URL
*
* @param url
* @return
*/
public static Boolean checkUrl(String url) {
List<Pattern> list = new LinkedList<>();
list.add(Pattern.compile("^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$"));
Matcher matcher;
Boolean result = false;
for (Pattern pattern : list) {
matcher = pattern.matcher(url); //遍历匹配
result |= matcher.matches(); //对所有的匹配结果做或运算。
}
return result;
}
}

@ -1,64 +0,0 @@
package com.wb.excel.api.util;
import com.wb.excel.api.entity.DataVerifyResult;
import com.wb.excel.api.entity.ExcelVerifyEntity;
import com.wb.excel.api.interfaces.IExcelVerifyHandler;
/**
* ***************************************************************
* <p/>
* <pre>
* Copyright (c) 2014
* ColumnDescription:
* ***************************************************************
* </pre>
*/
public class VerifyDataUtil {
private final static DataVerifyResult DEFAULT_RESULT = new DataVerifyResult(
true);
private void addVerifyResult(DataVerifyResult hanlderResult,
DataVerifyResult result) {
if (!hanlderResult.isSuccess()) {
result.setSuccess(false);
result.setMsg((StringUtils.isEmpty(result.getMsg()) ? "" : result.getMsg() + " , ")
+ hanlderResult.getMsg());
}
}
public DataVerifyResult verifyData(Object object, Object value, String name, String showName,
ExcelVerifyEntity verify, IExcelVerifyHandler excelVerifyHandler) {
if (verify == null) {
return DEFAULT_RESULT;
}
DataVerifyResult result = new DataVerifyResult(true, "");
if (verify.isNotNull()) {
addVerifyResult(BaseVerifyUtil.notNull(showName, value), result);
}
if (verify.isEmail()) {
addVerifyResult(BaseVerifyUtil.isEmail(showName, value), result);
}
if (verify.isMobile()) {
addVerifyResult(BaseVerifyUtil.isMobile(showName, value), result);
}
if (verify.isTel()) {
addVerifyResult(BaseVerifyUtil.isTel(showName, value), result);
}
if (verify.getMaxLength() != -1) {
addVerifyResult(BaseVerifyUtil.maxLength(showName, value, verify.getMaxLength()), result);
}
if (verify.getMinLength() != -1) {
addVerifyResult(BaseVerifyUtil.minLength(showName, value, verify.getMinLength()), result);
}
if (StringUtils.isNotEmpty(verify.getRegex())) {
addVerifyResult(
BaseVerifyUtil.regex(showName, value, verify.getRegex(), verify.getRegexTip()),
result);
}
if (verify.isInterHandler()) {
addVerifyResult(excelVerifyHandler.verifyHandler(object, name, value), result);
}
return result;
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.