Commit 3b89c0e4691e5355695b50ec7b8ec49588a12f83

Authored by wangbin
1 parent 61a8cc09
Exists in master

upgrate poi version

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.66'
  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/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 = "__";
... ...