PoiFunctionUtil.java
5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.bckefu.excel.util;
import com.bckefu.excel.exception.ExcelExportException;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;
/**
* @author caoliang1918@aliyun.com
* @Date 2017/11/5:23:11
*/
public final class PoiFunctionUtil {
private static final String TWO_DECIMAL_STR = "###.00";
private static final String THREE_DECIMAL_STR = "###.000";
private static final DecimalFormat TWO_DECIMAL = new DecimalFormat(TWO_DECIMAL_STR);
private static final DecimalFormat THREE_DECIMAL = new DecimalFormat(THREE_DECIMAL_STR);
private static final String DAY_STR = "yyyy-MM-dd";
private static final String TIME_STR = "yyyy-MM-dd HH:mm:ss";
private static final String TIME__NO_S_STR = "yyyy-MM-dd HH:mm";
private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat(DAY_STR);
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat(TIME_STR);
private static final SimpleDateFormat TIME__NO_S_FORMAT = new SimpleDateFormat(TIME__NO_S_STR);
private PoiFunctionUtil() {
}
/**
* 获取对象的长度
*
* @param obj
* @return
*/
@SuppressWarnings("rawtypes")
public static int length(Object obj) {
if (obj == null) {
return 0;
}
if (obj instanceof Map) {
return ((Map) obj).size();
}
if (obj instanceof Collection) {
return ((Collection) obj).size();
}
if (obj.getClass().isArray()) {
return Array.getLength(obj);
}
return String.valueOf(obj).length();
}
/**
* 格式化数字
*
* @param obj
* @return
* @throws NumberFormatException
*/
public static String formatNumber(Object obj, String format) {
if (obj == null || obj.toString() == "") {
return "";
}
double number = Double.valueOf(obj.toString());
DecimalFormat decimalFormat = null;
if (TWO_DECIMAL.equals(format)) {
decimalFormat = TWO_DECIMAL;
} else if (THREE_DECIMAL_STR.equals(format)) {
decimalFormat = THREE_DECIMAL;
} else {
decimalFormat = new DecimalFormat(format);
}
return decimalFormat.format(number);
}
/**
* 格式化时间
*
* @param obj
* @return
*/
public static String formatDate(Object obj, String format) {
if (obj == null || obj.toString() == "") {
return "";
}
SimpleDateFormat dateFormat = null;
if (DAY_STR.equals(format)) {
dateFormat = new SimpleDateFormat(DAY_STR);
} else if (TIME_STR.equals(format)) {
dateFormat = new SimpleDateFormat(TIME_STR);
} else if (TIME__NO_S_STR.equals(format)) {
dateFormat = new SimpleDateFormat(TIME__NO_S_STR);
} else {
dateFormat = new SimpleDateFormat(format);
}
return dateFormat.format(obj);
}
/**
* 判断是不是成功
*
* @param first
* @param operator
* @param second
* @return
*/
public static boolean isTrue(Object first, String operator, Object second) {
if (">".endsWith(operator)) {
return isGt(first, second);
} else if ("<".endsWith(operator)) {
return isGt(second, first);
} else if ("==".endsWith(operator)) {
if (first != null && second != null) {
return eq(first, second);
}
return first == second;
} else if ("!=".endsWith(operator)) {
if (first != null && second != null) {
return !first.equals(second);
}
return first != second;
} else {
throw new ExcelExportException("占不支持改操作符");
}
}
/**
* 判断两个对象是不是相等
*
* @param first
* @param second
* @return
*/
private static boolean eq(Object first, Object second) {
//要求两个对象当中至少一个对象不是字符串才进行数字类型判断
if (!(first instanceof String) || !(second instanceof String)) {
try {
double f = Double.parseDouble(first.toString());
double s = Double.parseDouble(second.toString());
return f == s;
} catch (NumberFormatException e) {
//可能存在的错误,忽略继续进行
}
}
return first.equals(second);
}
/**
* 前者是不是大于后者
*
* @param first
* @param second
* @return
*/
private static boolean isGt(Object first, Object second) {
if (first == null || first.toString() == "") {
return false;
}
if (second == null || second.toString() == "") {
return true;
}
double one = Double.valueOf(first.toString());
double two = Double.valueOf(second.toString());
return one > two;
}
}