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
| ... | ... | @@ -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 @@ |
| 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 | 23 | \ No newline at end of file | ... | ... |
src/main/java/com/taover/util/UtilExcel.java
| ... | ... | @@ -10,7 +10,6 @@ import java.util.HashMap; |
| 10 | 10 | import java.util.List; |
| 11 | 11 | import java.util.Map; |
| 12 | 12 | |
| 13 | -import org.apache.poi.hssf.usermodel.HSSFDataFormatter; | |
| 14 | 13 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 15 | 14 | import org.apache.poi.openxml4j.util.ZipSecureFile; |
| 16 | 15 | import org.apache.poi.ss.usermodel.Cell; |
| ... | ... | @@ -134,15 +133,7 @@ public class UtilExcel { |
| 134 | 133 | for(int j=0; j<dataRow.size(); ++j){ |
| 135 | 134 | Cell cell = row.createCell(j); |
| 136 | 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 | 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 | 175 | * 创建并保存excel表 sheet |
| ... | ... | @@ -192,15 +194,7 @@ public class UtilExcel { |
| 192 | 194 | for(int k=0; k<dataRow.size(); ++k){ |
| 193 | 195 | Cell cell = row.createCell(k); |
| 194 | 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 | 249 | for(int j=0; j<dataRow.size(); ++j){ |
| 256 | 250 | Cell cell = row.createCell(j); |
| 257 | 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 | 253 | if(backColor != null){ |
| 268 | 254 | cell.setCellStyle(style); |
| 269 | 255 | } | ... | ... |
src/main/java/com/taover/util/UtilString.java
| ... | ... | @@ -20,6 +20,8 @@ public class UtilString { |
| 20 | 20 | return ""; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | + source = replaceCodePage(source, ""); | |
| 24 | + source = replaceNoBreakBackspace(source, ""); | |
| 23 | 25 | Pattern pattern = Pattern.compile("\\S"); |
| 24 | 26 | int startIndex = -1; |
| 25 | 27 | for(int i=0; i<source.length(); ++i){ |
| ... | ... | @@ -44,6 +46,8 @@ public class UtilString { |
| 44 | 46 | return ""; |
| 45 | 47 | } |
| 46 | 48 | |
| 49 | + source = replaceCodePage(source, ""); | |
| 50 | + source = replaceNoBreakBackspace(source, ""); | |
| 47 | 51 | Pattern pattern = Pattern.compile("\\S"); |
| 48 | 52 | int endIndex = source.length()-1; |
| 49 | 53 | for(int i=source.length()-1; i>=0; --i){ |
| ... | ... | @@ -93,10 +97,19 @@ public class UtilString { |
| 93 | 97 | return ""; |
| 94 | 98 | } |
| 95 | 99 | |
| 100 | + @Deprecated | |
| 96 | 101 | public static String trimCodePage(String data){ |
| 97 | 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 | 114 | * 在compares字符数组查找pattern字符串,找到则返回字串在数组中的索引,未找到返回-1 |
| 102 | 115 | * @param pattern | ... | ... |
src/test/java/TempExcel.java
| 1 | 1 | import java.io.File; |
| 2 | 2 | import java.util.ArrayList; |
| 3 | +import java.util.Collections; | |
| 4 | +import java.util.Date; | |
| 3 | 5 | import java.util.HashMap; |
| 4 | 6 | import java.util.Iterator; |
| 5 | 7 | import java.util.List; |
| ... | ... | @@ -9,7 +11,23 @@ import com.taover.util.UtilExcel; |
| 9 | 11 | |
| 10 | 12 | public class TempExcel { |
| 11 | 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 | 33 | public static void checkExcelError() { | ... | ... |