1.pom依赖
org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml-schemas
3.14
org.apache.poi
poi-ooxml
3.14
2.CommonUtil工具类(为方便运算)
public class CommonUtil {
public static boolean isEmpty(String s) {
return s == null || s.equals("");
}
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof String) {
String instance = (String) obj;
if (instance.trim().length() <= 0 || "null".equalsIgnoreCase(instance)) {
return true;
}
} else if (obj instanceof Integer) {
Integer instance = (Integer) obj;
if (instance < 0) {
return true;
}
} else if (obj instanceof List>) {
List> instance = (List>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Map, ?>) {
Map, ?> instance = (Map, ?>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Object[]) {
Object[] instance = (Object[]) obj;
if (instance.length <= 0) {
return true;
}
} else if (obj instanceof Long) {
Long instance = (Long) obj;
if (instance < 0) {
return true;
}
}
return false;
}
public static boolean notEmpty(Object obj) {
return !isEmpty(obj);
}
public static boolean exactEqual(String source, String target) {
if (source == null || target == null) {
return false;
}
if (source.length() != target.length()) {
return false;
}
char[] sc = source.toCharArray();
char[] tc = target.toCharArray();
for (int i = 0; i < sc.length; i++) {
if (sc[i] == tc[i]) {
continue;
} else {
return false;
}
}
return true;
}
public static boolean isNumber(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]*");
if (str.indexOf(".") > 0) {// 判断是否有小数点
if (str.indexOf(".") == str.lastIndexOf(".") && str.split("\.").length == 2) { // 判断是否只有一个小数点
return pattern.matcher(str.replace(".", "")).matches();
} else {
return false;
}
} else {
return pattern.matcher(str).matches();
}
}
public static boolean isInteger(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\+]?[\d]*$");
return pattern.matcher(str).matches();
}
public static boolean isDouble(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\+]?\d*[.]\d+$"); // 之前这里正则表达式错误,现更正
return pattern.matcher(str).matches();
}
public static boolean isDate(String date) {
Pattern p = Pattern.compile("^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
return p.matcher(date).matches();
}
public static Date parseDate(String string) {
if (notEmpty(string)) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(string);
} catch (ParseException e) {
}
}
return null;
}
public static String join(String separator, List strList) {
StringBuilder sb = new StringBuilder();
for (String string : strList) {
sb.append(separator + string);
}
String saveStr = sb.toString();
return saveStr.length() > 0 ? saveStr.substring(separator.length()) : "";
}
public static boolean isOutLength(String s, int l) {
if (null == s || 0 == s.length()) {
return false;
}
if (s.length() > l) {
return true;
}
return false;
}
public static String extractText(String htmlStr) {
// 定义script的正则表达式{或