Commit e5b28cab35e736912e17c671a9efb1ed35ee48fc
Exists in
master
Merge branch 'master' of gitlab.taover.com:taov-erp/com-taover-util
Showing
6 changed files
with
112 additions
and
30 deletions
Show diff stats
build.gradle
@@ -59,7 +59,7 @@ uploadArchives { | @@ -59,7 +59,7 @@ uploadArchives { | ||
59 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 59 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
60 | } | 60 | } |
61 | pom.project { | 61 | pom.project { |
62 | - version '1.1.98' | 62 | + version '1.1.101' |
63 | artifactId ARTIFACT_Id | 63 | artifactId ARTIFACT_Id |
64 | groupId GROUP_ID | 64 | groupId GROUP_ID |
65 | packaging TYPE | 65 | packaging TYPE |
@@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +import java.util.Map; | ||
4 | +import java.util.concurrent.ConcurrentHashMap; | ||
5 | + | ||
6 | + | ||
7 | +public enum BasicType { | ||
8 | + BYTE, SHORT, INT, INTEGER, LONG, DOUBLE, FLOAT, BOOLEAN, CHAR, CHARACTER, STRING; | ||
9 | + | ||
10 | + public static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new ConcurrentHashMap<>(8); | ||
11 | + public static final Map<Class<?>, Class<?>> primitiveWrapperMap = new ConcurrentHashMap<>(8); | ||
12 | + | ||
13 | + static { | ||
14 | + wrapperPrimitiveMap.put(Boolean.class, boolean.class); | ||
15 | + wrapperPrimitiveMap.put(Byte.class, byte.class); | ||
16 | + wrapperPrimitiveMap.put(Character.class, char.class); | ||
17 | + wrapperPrimitiveMap.put(Double.class, double.class); | ||
18 | + wrapperPrimitiveMap.put(Float.class, float.class); | ||
19 | + wrapperPrimitiveMap.put(Integer.class, int.class); | ||
20 | + wrapperPrimitiveMap.put(Long.class, long.class); | ||
21 | + wrapperPrimitiveMap.put(Short.class, short.class); | ||
22 | + | ||
23 | + for (Map.Entry<Class<?>, Class<?>> entry : wrapperPrimitiveMap.entrySet()) { | ||
24 | + primitiveWrapperMap.put(entry.getValue(), entry.getKey()); | ||
25 | + } | ||
26 | + } | ||
27 | + | ||
28 | + public static Class<?> wrap(Class<?> clazz){ | ||
29 | + if(null == clazz || false == clazz.isPrimitive()){ | ||
30 | + return clazz; | ||
31 | + } | ||
32 | + Class<?> result = primitiveWrapperMap.get(clazz); | ||
33 | + return (null == result) ? clazz : result; | ||
34 | + } | ||
35 | + | ||
36 | + public static Class<?> unWrap(Class<?> clazz){ | ||
37 | + if(null == clazz || clazz.isPrimitive()){ | ||
38 | + return clazz; | ||
39 | + } | ||
40 | + Class<?> result = wrapperPrimitiveMap.get(clazz); | ||
41 | + return (null == result) ? clazz : result; | ||
42 | + } | ||
43 | +} |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +package com.taover.util; | ||
2 | + | ||
3 | +/** | ||
4 | + * 从HuTool那里COPY过来的 | ||
5 | + * @author Administrator | ||
6 | + * | ||
7 | + */ | ||
8 | +public class ClassUtil { | ||
9 | + public static boolean isPrimitiveWrapper(Class<?> clazz) { | ||
10 | + if (null == clazz) { | ||
11 | + return false; | ||
12 | + } | ||
13 | + return BasicType.wrapperPrimitiveMap.containsKey(clazz); | ||
14 | + } | ||
15 | + | ||
16 | + public static boolean isBasicType(Class<?> clazz) { | ||
17 | + if (null == clazz) { | ||
18 | + return false; | ||
19 | + } | ||
20 | + return (clazz.isPrimitive() || isPrimitiveWrapper(clazz)); | ||
21 | + } | ||
22 | +} | ||
0 | \ No newline at end of file | 23 | \ No newline at end of file |
src/main/java/com/taover/util/UtilExcel.java
@@ -10,7 +10,6 @@ import java.util.HashMap; | @@ -10,7 +10,6 @@ import java.util.HashMap; | ||
10 | import java.util.List; | 10 | import java.util.List; |
11 | import java.util.Map; | 11 | import java.util.Map; |
12 | 12 | ||
13 | -import org.apache.poi.hssf.usermodel.HSSFDataFormatter; | ||
14 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; | 13 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
15 | import org.apache.poi.openxml4j.util.ZipSecureFile; | 14 | import org.apache.poi.openxml4j.util.ZipSecureFile; |
16 | import org.apache.poi.ss.usermodel.Cell; | 15 | import org.apache.poi.ss.usermodel.Cell; |
@@ -134,15 +133,7 @@ public class UtilExcel { | @@ -134,15 +133,7 @@ public class UtilExcel { | ||
134 | for(int j=0; j<dataRow.size(); ++j){ | 133 | for(int j=0; j<dataRow.size(); ++j){ |
135 | Cell cell = row.createCell(j); | 134 | Cell cell = row.createCell(j); |
136 | Object dataCell = dataRow.get(j); | 135 | Object dataCell = dataRow.get(j); |
137 | - if(dataCell != null){ | ||
138 | - if(dataCell.getClass().isPrimitive()){ | ||
139 | - cell.setCellValue(Double.valueOf(dataCell.toString())); | ||
140 | - }else if(dataCell.getClass().getSimpleName().equals("Date")){ | ||
141 | - cell.setCellValue((Date)dataCell); | ||
142 | - }else{ | ||
143 | - cell.setCellValue(dataCell.toString()); | ||
144 | - } | ||
145 | - } | 136 | + setCellValue(cell, dataCell); |
146 | } | 137 | } |
147 | } | 138 | } |
148 | } | 139 | } |
@@ -168,6 +159,17 @@ public class UtilExcel { | @@ -168,6 +159,17 @@ public class UtilExcel { | ||
168 | } | 159 | } |
169 | } | 160 | } |
170 | 161 | ||
162 | + private static void setCellValue(Cell cell, Object data) { | ||
163 | + if(data != null){ | ||
164 | + if(ClassUtil.isBasicType(data.getClass())){ | ||
165 | + cell.setCellValue(Double.valueOf(data.toString())); | ||
166 | + }else if(data.getClass().getSimpleName().equals("Date")){ | ||
167 | + cell.setCellValue((Date)data); | ||
168 | + }else{ | ||
169 | + cell.setCellValue(data.toString()); | ||
170 | + } | ||
171 | + } | ||
172 | + } | ||
171 | 173 | ||
172 | /** | 174 | /** |
173 | * 创建并保存excel表 sheet | 175 | * 创建并保存excel表 sheet |
@@ -192,15 +194,7 @@ public class UtilExcel { | @@ -192,15 +194,7 @@ public class UtilExcel { | ||
192 | for(int k=0; k<dataRow.size(); ++k){ | 194 | for(int k=0; k<dataRow.size(); ++k){ |
193 | Cell cell = row.createCell(k); | 195 | Cell cell = row.createCell(k); |
194 | Object dataCell = dataRow.get(k); | 196 | Object dataCell = dataRow.get(k); |
195 | - if(dataCell != null){ | ||
196 | - if(dataCell.getClass().isPrimitive()){ | ||
197 | - cell.setCellValue(Double.valueOf(dataCell.toString())); | ||
198 | - }else if(dataCell.getClass().getSimpleName().equals("Date")){ | ||
199 | - cell.setCellValue((Date)dataCell); | ||
200 | - }else{ | ||
201 | - cell.setCellValue(dataCell.toString()); | ||
202 | - } | ||
203 | - } | 197 | + setCellValue(cell, dataCell); |
204 | } | 198 | } |
205 | } | 199 | } |
206 | } | 200 | } |
@@ -255,15 +249,7 @@ public class UtilExcel { | @@ -255,15 +249,7 @@ public class UtilExcel { | ||
255 | for(int j=0; j<dataRow.size(); ++j){ | 249 | for(int j=0; j<dataRow.size(); ++j){ |
256 | Cell cell = row.createCell(j); | 250 | Cell cell = row.createCell(j); |
257 | Object dataCell = dataRow.get(j); | 251 | Object dataCell = dataRow.get(j); |
258 | - if(dataCell != null){ | ||
259 | - if(dataCell.getClass().isPrimitive()){ | ||
260 | - cell.setCellValue(Double.valueOf(dataCell.toString())); | ||
261 | - }else if(dataCell.getClass().getSimpleName().equals("Date")){ | ||
262 | - cell.setCellValue((Date)dataCell); | ||
263 | - }else{ | ||
264 | - cell.setCellValue(dataCell.toString()); | ||
265 | - } | ||
266 | - } | 252 | + setCellValue(cell, dataCell); |
267 | if(backColor != null){ | 253 | if(backColor != null){ |
268 | cell.setCellStyle(style); | 254 | cell.setCellStyle(style); |
269 | } | 255 | } |
src/main/java/com/taover/util/UtilString.java
@@ -20,6 +20,8 @@ public class UtilString { | @@ -20,6 +20,8 @@ public class UtilString { | ||
20 | return ""; | 20 | return ""; |
21 | } | 21 | } |
22 | 22 | ||
23 | + source = replaceCodePage(source, ""); | ||
24 | + source = replaceNoBreakBackspace(source, ""); | ||
23 | Pattern pattern = Pattern.compile("\\S"); | 25 | Pattern pattern = Pattern.compile("\\S"); |
24 | int startIndex = -1; | 26 | int startIndex = -1; |
25 | for(int i=0; i<source.length(); ++i){ | 27 | for(int i=0; i<source.length(); ++i){ |
@@ -44,6 +46,8 @@ public class UtilString { | @@ -44,6 +46,8 @@ public class UtilString { | ||
44 | return ""; | 46 | return ""; |
45 | } | 47 | } |
46 | 48 | ||
49 | + source = replaceCodePage(source, ""); | ||
50 | + source = replaceNoBreakBackspace(source, ""); | ||
47 | Pattern pattern = Pattern.compile("\\S"); | 51 | Pattern pattern = Pattern.compile("\\S"); |
48 | int endIndex = source.length()-1; | 52 | int endIndex = source.length()-1; |
49 | for(int i=source.length()-1; i>=0; --i){ | 53 | for(int i=source.length()-1; i>=0; --i){ |
@@ -93,10 +97,19 @@ public class UtilString { | @@ -93,10 +97,19 @@ public class UtilString { | ||
93 | return ""; | 97 | return ""; |
94 | } | 98 | } |
95 | 99 | ||
100 | + @Deprecated | ||
96 | public static String trimCodePage(String data){ | 101 | public static String trimCodePage(String data){ |
97 | return data.replaceAll(new String(new byte[]{-30, -128, -83}), ""); | 102 | return data.replaceAll(new String(new byte[]{-30, -128, -83}), ""); |
98 | } | 103 | } |
99 | 104 | ||
105 | + public static String replaceCodePage(String data, String replaceStr){ | ||
106 | + return data.replaceAll(new String(new byte[]{-30, -128, -83}), replaceStr); | ||
107 | + } | ||
108 | + | ||
109 | + public static String replaceNoBreakBackspace(String data, String replaceStr) { | ||
110 | + return data.replaceAll(new String(new byte[] {-62, -96}), replaceStr); | ||
111 | + } | ||
112 | + | ||
100 | /** | 113 | /** |
101 | * 在compares字符数组查找pattern字符串,找到则返回字串在数组中的索引,未找到返回-1 | 114 | * 在compares字符数组查找pattern字符串,找到则返回字串在数组中的索引,未找到返回-1 |
102 | * @param pattern | 115 | * @param pattern |
src/test/java/TempExcel.java
1 | import java.io.File; | 1 | import java.io.File; |
2 | import java.util.ArrayList; | 2 | import java.util.ArrayList; |
3 | +import java.util.Collections; | ||
4 | +import java.util.Date; | ||
3 | import java.util.HashMap; | 5 | import java.util.HashMap; |
4 | import java.util.Iterator; | 6 | import java.util.Iterator; |
5 | import java.util.List; | 7 | import java.util.List; |
@@ -9,7 +11,23 @@ import com.taover.util.UtilExcel; | @@ -9,7 +11,23 @@ import com.taover.util.UtilExcel; | ||
9 | 11 | ||
10 | public class TempExcel { | 12 | public class TempExcel { |
11 | public static void main(String[] args){ | 13 | public static void main(String[] args){ |
12 | - checkExcelError(); | 14 | + //checkExcelError(); |
15 | + saveExcel(); | ||
16 | + } | ||
17 | + | ||
18 | + public static void saveExcel() { | ||
19 | + List<List<Object>> data = new ArrayList<List<Object>>(); | ||
20 | + data.add(Collections.singletonList(123)); | ||
21 | + data.add(Collections.singletonList(123.222)); | ||
22 | + data.add(Collections.singletonList(1232223234234234L)); | ||
23 | + data.add(Collections.singletonList("123")); | ||
24 | + data.add(Collections.singletonList("1232223234234234")); | ||
25 | + data.add(Collections.singletonList(new Date())); | ||
26 | + try { | ||
27 | + UtilExcel.saveExcel("data", data, "C:\\Users\\Administrator\\Desktop\\1234.xlsx"); | ||
28 | + } catch (Exception e) { | ||
29 | + e.printStackTrace(); | ||
30 | + } | ||
13 | } | 31 | } |
14 | 32 | ||
15 | public static void checkExcelError() { | 33 | public static void checkExcelError() { |