diff --git a/build.gradle b/build.gradle index a9cb49e..9850f89 100644 --- a/build.gradle +++ b/build.gradle @@ -20,8 +20,8 @@ mainClassName = 'com.taover.util.UtilString' dependencies { compile( - "org.apache.poi:poi:3.16", - "org.apache.poi:poi-excelant:3.16", + "org.apache.poi:poi:4.1.2", + "org.apache.poi:poi-excelant:4.1.2", "ch.ethz.ganymed:ganymed-ssh2:build210", "org.apache.velocity:velocity:1.6.4", "com.squareup.okhttp3:okhttp:3.14.1", @@ -59,7 +59,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.66' + version '1.1.68' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilExcel.java b/src/main/java/com/taover/util/UtilExcel.java index 409db74..cfa3dde 100644 --- a/src/main/java/com/taover/util/UtilExcel.java +++ b/src/main/java/com/taover/util/UtilExcel.java @@ -1,7 +1,6 @@ package com.taover.util; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; @@ -16,9 +15,11 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class UtilExcel { @@ -37,34 +38,29 @@ public class UtilExcel { */ private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ Workbook wb = null; + File tempFile = null; if(isRead){ - File tempFile = new File(filePath); + tempFile = new File(filePath); if(!tempFile.exists()){ throw new Exception("需要读取的文件不存在!"); } } - String fileType = filePath.substring(filePath.lastIndexOf(".")); - if(excel2003L.equals(fileType.trim().toLowerCase())){ - if(isRead){ - wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- - }else{ - wb = new HSSFWorkbook(); //2003- - } - }else if(excel2007U.equals(fileType.trim().toLowerCase())){ - if(isRead){ - wb = new XSSFWorkbook(new FileInputStream(filePath)); //2007+ - }else{ - wb = new XSSFWorkbook(); //2007+ - } - }else if(CSV.equals(fileType.trim().toLowerCase())){ - if(isRead){ - wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- - }else{ - wb = new HSSFWorkbook(); //2003- + if(isRead) { + wb = WorkbookFactory.create(tempFile); + }else { + int dotIndex = filePath.lastIndexOf("."); + if(dotIndex < 0) { + throw new Exception("传入的文件没有指定扩展名"); } - } else{ - throw new Exception("解析的文件格式有误!"); - } + String filteTypeLower = filePath.substring(dotIndex).trim().toLowerCase(); + if(excel2003L.equals(filteTypeLower) || CSV.equals(filteTypeLower)){ + wb = new HSSFWorkbook(); //2003- + } else if (excel2007U.equals(filteTypeLower)){ + wb = new XSSFWorkbook(); //2007+ + } else { + throw new Exception("解析的文件格式有误!"); + } + } return wb; } @@ -244,7 +240,7 @@ public class UtilExcel { CellStyle style = cellStyleMap.get(backColor); if(style == null){ style = wb.createCellStyle(); - style.setFillPattern(style.SOLID_FOREGROUND); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyleMap.put(backColor, style); } if(backColor != null){ diff --git a/src/test/java/TempExcel.java b/src/test/java/TempExcel.java index 13d672c..661bd7f 100644 --- a/src/test/java/TempExcel.java +++ b/src/test/java/TempExcel.java @@ -1,3 +1,4 @@ +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -8,7 +9,18 @@ import com.taover.util.UtilExcel; public class TempExcel { public static void main(String[] args){ - dealExcel(); + //dealExcel(); + readExcel(); + } + + private static void readExcel() { + File file = new File("C:\\Users\\Administrator\\Desktop\\异常Excel\\1_2020-03-08-14h55m00s-面包仓-订单回传数据(2)(1).xlsx"); + try { + Map>> readExcelAllSheetMap = UtilExcel.readExcelAllSheetMap(file.getAbsolutePath()); + System.out.println("12"); + } catch (Exception e) { + e.printStackTrace(); + } } public static final String SEPARATE_CONSIGNEE_MOBILE = "__"; -- libgit2 0.21.2