Commit f78fc6a6b62703ff58a23e80d29cba35f41b8592

Authored by unknown
1 parent 4ce30fea
Exists in master

1.提交Excel文件生成代码优化

build.gradle
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.96'
  62 + version '1.1.99'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/BasicType.java 0 → 100644
... ... @@ -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 +}
... ...
src/main/java/com/taover/util/ClassUtil.java 0 → 100644
... ... @@ -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
... ... @@ -133,15 +133,7 @@ public class UtilExcel {
133 133 for(int j=0; j<dataRow.size(); ++j){
134 134 Cell cell = row.createCell(j);
135 135 Object dataCell = dataRow.get(j);
136   - if(dataCell != null){
137   - if(dataCell.getClass().isPrimitive()){
138   - cell.setCellValue(Double.valueOf(dataCell.toString()));
139   - }else if(dataCell.getClass().getSimpleName().equals("Date")){
140   - cell.setCellValue((Date)dataCell);
141   - }else{
142   - cell.setCellValue(dataCell.toString());
143   - }
144   - }
  136 + setCellValue(cell, dataCell);
145 137 }
146 138 }
147 139 }
... ... @@ -167,6 +159,17 @@ public class UtilExcel {
167 159 }
168 160 }
169 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 + }
170 173  
171 174 /**
172 175 * 创建并保存excel表 sheet
... ... @@ -191,15 +194,7 @@ public class UtilExcel {
191 194 for(int k=0; k<dataRow.size(); ++k){
192 195 Cell cell = row.createCell(k);
193 196 Object dataCell = dataRow.get(k);
194   - if(dataCell != null){
195   - if(dataCell.getClass().isPrimitive()){
196   - cell.setCellValue(Double.valueOf(dataCell.toString()));
197   - }else if(dataCell.getClass().getSimpleName().equals("Date")){
198   - cell.setCellValue((Date)dataCell);
199   - }else{
200   - cell.setCellValue(dataCell.toString());
201   - }
202   - }
  197 + setCellValue(cell, dataCell);
203 198 }
204 199 }
205 200 }
... ... @@ -254,15 +249,7 @@ public class UtilExcel {
254 249 for(int j=0; j<dataRow.size(); ++j){
255 250 Cell cell = row.createCell(j);
256 251 Object dataCell = dataRow.get(j);
257   - if(dataCell != null){
258   - if(dataCell.getClass().isPrimitive()){
259   - cell.setCellValue(Double.valueOf(dataCell.toString()));
260   - }else if(dataCell.getClass().getSimpleName().equals("Date")){
261   - cell.setCellValue((Date)dataCell);
262   - }else{
263   - cell.setCellValue(dataCell.toString());
264   - }
265   - }
  252 + setCellValue(cell, dataCell);
266 253 if(backColor != null){
267 254 cell.setCellStyle(style);
268 255 }
... ...
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() {
... ...