Commit fbeeb88c42ffc3c93a416bf5ff25307283951c4e

Authored by gaoming
2 parents 5349d013 3b89c0e4
Exists in master

Merge branch 'master' of gitlab.taover.com:taov-erp/com-taover-util

build.gradle
... ... @@ -20,8 +20,8 @@ mainClassName = 'com.taover.util.UtilString'
20 20  
21 21 dependencies {
22 22 compile(
23   - "org.apache.poi:poi:3.16",
24   - "org.apache.poi:poi-excelant:3.16",
  23 + "org.apache.poi:poi:4.1.2",
  24 + "org.apache.poi:poi-excelant:4.1.2",
25 25 "ch.ethz.ganymed:ganymed-ssh2:build210",
26 26 "org.apache.velocity:velocity:1.6.4",
27 27 "com.squareup.okhttp3:okhttp:3.14.1",
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.39'
  62 + version '1.1.68'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/UtilExcel.java
1 1 package com.taover.util;
2 2  
3 3 import java.io.File;
4   -import java.io.FileInputStream;
5 4 import java.io.FileOutputStream;
6 5 import java.text.DecimalFormat;
7 6 import java.text.SimpleDateFormat;
... ... @@ -16,9 +15,11 @@ import org.apache.poi.ss.usermodel.Cell;
16 15 import org.apache.poi.ss.usermodel.CellStyle;
17 16 import org.apache.poi.ss.usermodel.CellType;
18 17 import org.apache.poi.ss.usermodel.DateUtil;
  18 +import org.apache.poi.ss.usermodel.FillPatternType;
19 19 import org.apache.poi.ss.usermodel.Row;
20 20 import org.apache.poi.ss.usermodel.Sheet;
21 21 import org.apache.poi.ss.usermodel.Workbook;
  22 +import org.apache.poi.ss.usermodel.WorkbookFactory;
22 23 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23 24  
24 25 public class UtilExcel {
... ... @@ -37,34 +38,29 @@ public class UtilExcel {
37 38 */
38 39 private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{
39 40 Workbook wb = null;
  41 + File tempFile = null;
40 42 if(isRead){
41   - File tempFile = new File(filePath);
  43 + tempFile = new File(filePath);
42 44 if(!tempFile.exists()){
43 45 throw new Exception("需要读取的文件不存在!");
44 46 }
45 47 }
46   - String fileType = filePath.substring(filePath.lastIndexOf("."));
47   - if(excel2003L.equals(fileType.trim().toLowerCase())){
48   - if(isRead){
49   - wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003-
50   - }else{
51   - wb = new HSSFWorkbook(); //2003-
52   - }
53   - }else if(excel2007U.equals(fileType.trim().toLowerCase())){
54   - if(isRead){
55   - wb = new XSSFWorkbook(new FileInputStream(filePath)); //2007+
56   - }else{
57   - wb = new XSSFWorkbook(); //2007+
58   - }
59   - }else if(CSV.equals(fileType.trim().toLowerCase())){
60   - if(isRead){
61   - wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003-
62   - }else{
63   - wb = new HSSFWorkbook(); //2003-
  48 + if(isRead) {
  49 + wb = WorkbookFactory.create(tempFile);
  50 + }else {
  51 + int dotIndex = filePath.lastIndexOf(".");
  52 + if(dotIndex < 0) {
  53 + throw new Exception("传入的文件没有指定扩展名");
64 54 }
65   - } else{
66   - throw new Exception("解析的文件格式有误!");
67   - }
  55 + String filteTypeLower = filePath.substring(dotIndex).trim().toLowerCase();
  56 + if(excel2003L.equals(filteTypeLower) || CSV.equals(filteTypeLower)){
  57 + wb = new HSSFWorkbook(); //2003-
  58 + } else if (excel2007U.equals(filteTypeLower)){
  59 + wb = new XSSFWorkbook(); //2007+
  60 + } else {
  61 + throw new Exception("解析的文件格式有误!");
  62 + }
  63 + }
68 64 return wb;
69 65 }
70 66  
... ... @@ -244,7 +240,7 @@ public class UtilExcel {
244 240 CellStyle style = cellStyleMap.get(backColor);
245 241 if(style == null){
246 242 style = wb.createCellStyle();
247   - style.setFillPattern(style.SOLID_FOREGROUND);
  243 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
248 244 cellStyleMap.put(backColor, style);
249 245 }
250 246 if(backColor != null){
... ...
src/main/java/com/taover/util/UtilJSON.java
... ... @@ -6,6 +6,19 @@ import net.sf.json.JSONArray;
6 6 import net.sf.json.JSONObject;
7 7  
8 8 public class UtilJSON {
  9 + public static boolean isJsonNull(Object data){
  10 + if(data == null){
  11 + return true;
  12 + }
  13 + if(!(data instanceof String) && data.toString().equals("null")){
  14 + return true;
  15 + }
  16 + if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){
  17 + return true;
  18 + }
  19 + return false;
  20 + }
  21 +
9 22 public static void removeJsonNull(JSONObject data){
10 23 if(data == null || data.isNullObject()){
11 24 return;
... ... @@ -16,44 +29,76 @@ public class UtilJSON {
16 29 Object value = data.get(keyItem);
17 30  
18 31 if(isJsonNull(value)) {
19   - data.put(keyItem, "");
  32 + data.remove(keyItem);
  33 + continue;
20 34 }
21 35  
22 36 if(value instanceof JSONObject){
23 37 removeJsonNull((JSONObject)value);
  38 + }else if(value instanceof JSONArray){
  39 + removeJsonNull((JSONArray)value);
  40 + }
  41 + }
  42 + }
  43 +
  44 + public static void removeJsonNull(JSONArray dataArr){
  45 + if(dataArr == null){
  46 + return;
  47 + }
  48 + for(int i=0; i<dataArr.size(); ++i){
  49 + Object dataItem = dataArr.get(i);
  50 + if(isJsonNull(dataItem)){
  51 + dataArr.remove(i);
  52 + --i;
24 53 continue;
25 54 }
26 55  
27   - if(value instanceof JSONArray){
28   - removeJsonNull((JSONArray)value);
  56 + if(dataItem instanceof JSONObject){
  57 + removeJsonNull((JSONObject)dataItem);
  58 + }else if(dataItem instanceof JSONArray){
  59 + removeJsonNull((JSONArray)dataItem);
29 60 }
30 61 }
31 62 }
32 63  
33   - public static boolean isJsonNull(Object data){
34   - if(data == null){
35   - return true;
36   - }
37   - if(!(data instanceof String) && data.toString().equals("null")){
38   - return true;
  64 + public static void replaceJsonNull(JSONObject data, Object target){
  65 + if(data == null || data.isNullObject()){
  66 + return;
39 67 }
40   - if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){
41   - return true;
  68 + Iterator<String> keyIter = data.keys();
  69 + while(keyIter.hasNext()){
  70 + String keyItem = keyIter.next();
  71 + Object value = data.get(keyItem);
  72 +
  73 + if(isJsonNull(value)) {
  74 + data.put(keyItem, target);
  75 + continue;
  76 + }
  77 +
  78 + if(value instanceof JSONObject){
  79 + replaceJsonNull((JSONObject)value, target);
  80 + }else if(value instanceof JSONArray){
  81 + replaceJsonNull((JSONArray)value, target);
  82 + }
42 83 }
43   - return false;
44 84 }
45   -
46   - public static void removeJsonNull(JSONArray dataArr){
  85 +
  86 + public static void replaceJsonNull(JSONArray dataArr, Object target){
47 87 if(dataArr == null){
48 88 return;
49 89 }
50 90 for(int i=0; i<dataArr.size(); ++i){
51   - JSONObject dataItem = dataArr.optJSONObject(i);
52   - if(dataItem.isNullObject()){
53   - dataArr.remove(i);
  91 + Object dataItem = dataArr.get(i);
  92 + if(isJsonNull(dataItem)){
  93 + dataArr.set(i, target);
54 94 continue;
55 95 }
56   - removeJsonNull(dataItem);
  96 +
  97 + if(dataItem instanceof JSONObject){
  98 + replaceJsonNull((JSONObject)dataItem, target);
  99 + }else if(dataItem instanceof JSONArray){
  100 + replaceJsonNull((JSONArray)dataItem, target);
  101 + }
57 102 }
58 103 }
59 104  
... ...
src/test/java/TempExcel.java
  1 +import java.io.File;
1 2 import java.util.ArrayList;
2 3 import java.util.HashMap;
3 4 import java.util.Iterator;
... ... @@ -8,7 +9,18 @@ import com.taover.util.UtilExcel;
8 9  
9 10 public class TempExcel {
10 11 public static void main(String[] args){
11   - dealExcel();
  12 + //dealExcel();
  13 + readExcel();
  14 + }
  15 +
  16 + private static void readExcel() {
  17 + File file = new File("C:\\Users\\Administrator\\Desktop\\异常Excel\\1_2020-03-08-14h55m00s-面包仓-订单回传数据(2)(1).xlsx");
  18 + try {
  19 + Map<String, List<List<Object>>> readExcelAllSheetMap = UtilExcel.readExcelAllSheetMap(file.getAbsolutePath());
  20 + System.out.println("12");
  21 + } catch (Exception e) {
  22 + e.printStackTrace();
  23 + }
12 24 }
13 25  
14 26 public static final String SEPARATE_CONSIGNEE_MOBILE = "__";
... ...