Commit 3b89c0e4691e5355695b50ec7b8ec49588a12f83

Authored by wangbin
1 parent 61a8cc09
Exists in master

upgrate poi version

@@ -20,8 +20,8 @@ mainClassName = 'com.taover.util.UtilString' @@ -20,8 +20,8 @@ mainClassName = 'com.taover.util.UtilString'
20 20
21 dependencies { 21 dependencies {
22 compile( 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 "ch.ethz.ganymed:ganymed-ssh2:build210", 25 "ch.ethz.ganymed:ganymed-ssh2:build210",
26 "org.apache.velocity:velocity:1.6.4", 26 "org.apache.velocity:velocity:1.6.4",
27 "com.squareup.okhttp3:okhttp:3.14.1", 27 "com.squareup.okhttp3:okhttp:3.14.1",
@@ -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.66' 62 + version '1.1.68'
63 artifactId ARTIFACT_Id 63 artifactId ARTIFACT_Id
64 groupId GROUP_ID 64 groupId GROUP_ID
65 packaging TYPE 65 packaging TYPE
src/main/java/com/taover/util/UtilExcel.java
1 package com.taover.util; 1 package com.taover.util;
2 2
3 import java.io.File; 3 import java.io.File;
4 -import java.io.FileInputStream;  
5 import java.io.FileOutputStream; 4 import java.io.FileOutputStream;
6 import java.text.DecimalFormat; 5 import java.text.DecimalFormat;
7 import java.text.SimpleDateFormat; 6 import java.text.SimpleDateFormat;
@@ -16,9 +15,11 @@ import org.apache.poi.ss.usermodel.Cell; @@ -16,9 +15,11 @@ import org.apache.poi.ss.usermodel.Cell;
16 import org.apache.poi.ss.usermodel.CellStyle; 15 import org.apache.poi.ss.usermodel.CellStyle;
17 import org.apache.poi.ss.usermodel.CellType; 16 import org.apache.poi.ss.usermodel.CellType;
18 import org.apache.poi.ss.usermodel.DateUtil; 17 import org.apache.poi.ss.usermodel.DateUtil;
  18 +import org.apache.poi.ss.usermodel.FillPatternType;
19 import org.apache.poi.ss.usermodel.Row; 19 import org.apache.poi.ss.usermodel.Row;
20 import org.apache.poi.ss.usermodel.Sheet; 20 import org.apache.poi.ss.usermodel.Sheet;
21 import org.apache.poi.ss.usermodel.Workbook; 21 import org.apache.poi.ss.usermodel.Workbook;
  22 +import org.apache.poi.ss.usermodel.WorkbookFactory;
22 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 23 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23 24
24 public class UtilExcel { 25 public class UtilExcel {
@@ -37,34 +38,29 @@ public class UtilExcel { @@ -37,34 +38,29 @@ public class UtilExcel {
37 */ 38 */
38 private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ 39 private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{
39 Workbook wb = null; 40 Workbook wb = null;
  41 + File tempFile = null;
40 if(isRead){ 42 if(isRead){
41 - File tempFile = new File(filePath); 43 + tempFile = new File(filePath);
42 if(!tempFile.exists()){ 44 if(!tempFile.exists()){
43 throw new Exception("需要读取的文件不存在!"); 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 return wb; 64 return wb;
69 } 65 }
70 66
@@ -244,7 +240,7 @@ public class UtilExcel { @@ -244,7 +240,7 @@ public class UtilExcel {
244 CellStyle style = cellStyleMap.get(backColor); 240 CellStyle style = cellStyleMap.get(backColor);
245 if(style == null){ 241 if(style == null){
246 style = wb.createCellStyle(); 242 style = wb.createCellStyle();
247 - style.setFillPattern(style.SOLID_FOREGROUND); 243 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
248 cellStyleMap.put(backColor, style); 244 cellStyleMap.put(backColor, style);
249 } 245 }
250 if(backColor != null){ 246 if(backColor != null){
src/test/java/TempExcel.java
  1 +import java.io.File;
1 import java.util.ArrayList; 2 import java.util.ArrayList;
2 import java.util.HashMap; 3 import java.util.HashMap;
3 import java.util.Iterator; 4 import java.util.Iterator;
@@ -8,7 +9,18 @@ import com.taover.util.UtilExcel; @@ -8,7 +9,18 @@ import com.taover.util.UtilExcel;
8 9
9 public class TempExcel { 10 public class TempExcel {
10 public static void main(String[] args){ 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 public static final String SEPARATE_CONSIGNEE_MOBILE = "__"; 26 public static final String SEPARATE_CONSIGNEE_MOBILE = "__";