Commit 4b046a0f754e5a2f9797bc74ef897985adadae91
Exists in
master
merge
Showing
5 changed files
with
217 additions
and
54 deletions
Show diff stats
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.36' | |
| 62 | + version '1.1.76' | |
| 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 { |
| ... | ... | @@ -27,7 +28,7 @@ public class UtilExcel { |
| 27 | 28 | private final static String CSV =".csv"; //csv |
| 28 | 29 | |
| 29 | 30 | private final static int maxExcelRowNum = 10000; |
| 30 | - private final static int maxExcelColumnNum = 50; | |
| 31 | + private final static int maxExcelColumnNum = 100; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * 描述:根据文件后缀,自适应上传文件的版本 |
| ... | ... | @@ -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 | |
| ... | ... | @@ -167,6 +163,66 @@ public class UtilExcel { |
| 167 | 163 | } |
| 168 | 164 | } |
| 169 | 165 | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * 创建并保存excel表 sheet | |
| 169 | + * @param sheetName | |
| 170 | + * @param data | |
| 171 | + * @param path | |
| 172 | + */ | |
| 173 | + public static void saveExcelContailSheet(List<String> sheetList, List<List<List<Object>>> dataList, String path) throws Exception{ | |
| 174 | + if(sheetList.size() != dataList.size()){ | |
| 175 | + throw new Exception("sheet size != excel size"); | |
| 176 | + } | |
| 177 | + Workbook wb = UtilExcel.getWorkbook(path, false); | |
| 178 | + //创建Excel工作簿对象 | |
| 179 | + | |
| 180 | + for (int j = 0;j< dataList.size(); j++) { | |
| 181 | + List<List<Object>> data = dataList.get(j); | |
| 182 | + Sheet sheet = wb.createSheet(sheetList.get(j));//创建Excel工作表对象 | |
| 183 | + for(int i=0; i<data.size(); ++i){ | |
| 184 | + Row row = sheet.createRow(i); //创建Excel工作表的行 | |
| 185 | + List<Object> dataRow = data.get(i); | |
| 186 | + if(dataRow != null){ | |
| 187 | + for(int k=0; k<dataRow.size(); ++k){ | |
| 188 | + Cell cell = row.createCell(k); | |
| 189 | + Object dataCell = dataRow.get(k); | |
| 190 | + if(dataCell != null){ | |
| 191 | + if(dataCell.getClass().isPrimitive()){ | |
| 192 | + cell.setCellValue(Double.valueOf(dataCell.toString())); | |
| 193 | + }else if(dataCell.getClass().getSimpleName().equals("Date")){ | |
| 194 | + cell.setCellValue((Date)dataCell); | |
| 195 | + }else{ | |
| 196 | + cell.setCellValue(dataCell.toString()); | |
| 197 | + } | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + } | |
| 202 | + } | |
| 203 | + | |
| 204 | + try { | |
| 205 | + FileOutputStream fileOut; | |
| 206 | + File tempFile = new File(path); | |
| 207 | + if(!tempFile.exists()){ | |
| 208 | + File parentFile = tempFile.getParentFile(); | |
| 209 | + if(!parentFile.exists()){ | |
| 210 | + parentFile.mkdirs(); | |
| 211 | + } | |
| 212 | + if(!tempFile.createNewFile()){ | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + } | |
| 216 | + fileOut = new FileOutputStream(tempFile); | |
| 217 | + wb.write(fileOut); | |
| 218 | + wb.close(); | |
| 219 | + fileOut.close(); | |
| 220 | + } catch (Exception e) { | |
| 221 | + // TODO Auto-generated catch block | |
| 222 | + e.printStackTrace(); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + | |
| 170 | 226 | /** |
| 171 | 227 | * 创建并保存excel表 |
| 172 | 228 | * @param sheetName |
| ... | ... | @@ -184,7 +240,7 @@ public class UtilExcel { |
| 184 | 240 | CellStyle style = cellStyleMap.get(backColor); |
| 185 | 241 | if(style == null){ |
| 186 | 242 | style = wb.createCellStyle(); |
| 187 | - style.setFillPattern(style.SOLID_FOREGROUND); | |
| 243 | + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
| 188 | 244 | cellStyleMap.put(backColor, style); |
| 189 | 245 | } |
| 190 | 246 | if(backColor != null){ |
| ... | ... | @@ -291,6 +347,29 @@ public class UtilExcel { |
| 291 | 347 | |
| 292 | 348 | return result; |
| 293 | 349 | } |
| 350 | + | |
| 351 | + | |
| 352 | + /** | |
| 353 | + * 读取excel表--所有工作表 Map封装 | |
| 354 | + * @param path | |
| 355 | + */ | |
| 356 | + public static Map<String,List<List<Object>>> readExcelAllSheetMap(String filepath) throws Exception{ | |
| 357 | + File file = new File(filepath); | |
| 358 | + HashMap<String, List<List<Object>>> map = new HashMap<String,List<List<Object>>>(); | |
| 359 | + if(!file.exists()){ | |
| 360 | + throw new Exception("Excel["+filepath+"]文件不存在"); | |
| 361 | + } | |
| 362 | + Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
| 363 | + int sheetNumber = wb.getNumberOfSheets(); | |
| 364 | + | |
| 365 | + for(int sheetIndex=0; sheetIndex<sheetNumber; ++sheetIndex){ | |
| 366 | + map.put(sheetIndex+"",(readExcelBySheetIndexExcludeHideLine(filepath, sheetIndex))); | |
| 367 | + } | |
| 368 | + | |
| 369 | + return map; | |
| 370 | + } | |
| 371 | + | |
| 372 | + | |
| 294 | 373 | |
| 295 | 374 | /** |
| 296 | 375 | * 读取excel表--当前激活的工作表 |
| ... | ... | @@ -379,8 +458,9 @@ public class UtilExcel { |
| 379 | 458 | // 如果是date类型则 ,获取该cell的date值 |
| 380 | 459 | return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); |
| 381 | 460 | } else { |
| 382 | - // 纯数字 | |
| 383 | - return df.format(cell.getNumericCellValue()); | |
| 461 | + // 纯数字gaoming 新增兼容浮点型,转成字符串读取,不然小数点被省略了 | |
| 462 | + cell.setCellType(CellType.STRING); | |
| 463 | + return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
| 384 | 464 | } |
| 385 | 465 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
| 386 | 466 | return cell.getBooleanCellValue(); |
| ... | ... | @@ -421,18 +501,21 @@ public class UtilExcel { |
| 421 | 501 | |
| 422 | 502 | public static void main(String args[]){ |
| 423 | 503 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
| 424 | - String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xlsx"; | |
| 504 | + String filepath = "C:\\Users\\gaoming\\Desktop\\mmm.xlsx"; | |
| 425 | 505 | List<List<Object>> data = null; |
| 506 | + | |
| 426 | 507 | try { |
| 427 | - data = UtilExcel.readExcel(filepath); | |
| 508 | + Map<String, List<List<Object>>> map = UtilExcel.readExcelAllSheetMap(filepath); | |
| 509 | + System.out.println(map); | |
| 510 | + data = map.get("0"); | |
| 428 | 511 | System.out.println(data); |
| 429 | 512 | System.out.println(data.size()); |
| 430 | - System.out.println(UtilExcel.readExcelExcludeHideLine(filepath).size()); | |
| 513 | + System.out.println(UtilExcel.readExcelAllSheetMap(filepath)); | |
| 431 | 514 | } catch (Exception e) { |
| 432 | 515 | // TODO Auto-generated catch block |
| 433 | 516 | e.printStackTrace(); |
| 434 | 517 | } |
| 435 | -// List<Short> styleList = new ArrayList<Short>(); | |
| 518 | + List<Short> styleList = new ArrayList<Short>(); | |
| 436 | 519 | // for(int i=0; i<data.size(); ++i){ |
| 437 | 520 | // if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index)); |
| 438 | 521 | // else styleList.add(null); |
| ... | ... | @@ -442,7 +525,29 @@ public class UtilExcel { |
| 442 | 525 | // System.out.println(""); |
| 443 | 526 | // } |
| 444 | 527 | // try { |
| 445 | -// UtilExcel.saveExcel("测试", data, "D:\\12345.xlsx", styleList); | |
| 528 | +// List<String> headerList = new ArrayList<String>(); | |
| 529 | +// headerList.add("shhe1"); | |
| 530 | +// headerList.add("shhe2"); | |
| 531 | +// List<List<List<Object>>> dataList = new ArrayList<List<List<Object>>>(); | |
| 532 | +// List<List<Object>> list1 = new ArrayList<List<Object>>(); | |
| 533 | +// List<Object> list11 = new ArrayList<Object>(); | |
| 534 | +// list11.add("hahaha"); | |
| 535 | +// list11.add("hahaha"); | |
| 536 | +// list11.add("hahaha"); | |
| 537 | +// list1.add(list11); | |
| 538 | +// | |
| 539 | +// List<List<Object>> list2 = new ArrayList<List<Object>>(); | |
| 540 | +// List<Object> list22 = new ArrayList<Object>(); | |
| 541 | +// list22.add("hahaha2"); | |
| 542 | +// list22.add("hahaha2"); | |
| 543 | +// list22.add("hahaha2"); | |
| 544 | +// list2.add(list22); | |
| 545 | +// list2.add(list22); | |
| 546 | +// | |
| 547 | +// dataList.add(list1); | |
| 548 | +// dataList.add(list2); | |
| 549 | +// | |
| 550 | +// UtilExcel.saveExcelContailSheet(headerList, dataList, "C:\\Users\\gaoming\\Desktop\\qwer.xls"); | |
| 446 | 551 | // } catch (Exception e) { |
| 447 | 552 | // // TODO Auto-generated catch block |
| 448 | 553 | // e.printStackTrace(); | ... | ... |
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/main/java/com/taover/util/UtilString.java
| ... | ... | @@ -45,7 +45,7 @@ public class UtilString { |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | Pattern pattern = Pattern.compile("\\S"); |
| 48 | - int endIndex = source.length(); | |
| 48 | + int endIndex = source.length()-1; | |
| 49 | 49 | for(int i=source.length()-1; i>=0; --i){ |
| 50 | 50 | char data = source.charAt(i); |
| 51 | 51 | if(pattern.matcher(data+"").find()){ | ... | ... |
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; |
| ... | ... | @@ -19,6 +20,18 @@ public class TempExcel { |
| 19 | 20 | e.printStackTrace(); |
| 20 | 21 | } |
| 21 | 22 | System.out.println("end"); |
| 23 | + //dealExcel(); | |
| 24 | + readExcel(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + private static void readExcel() { | |
| 28 | + File file = new File("C:\\Users\\Administrator\\Desktop\\异常Excel\\1_2020-03-08-14h55m00s-面包仓-订单回传数据(2)(1).xlsx"); | |
| 29 | + try { | |
| 30 | + Map<String, List<List<Object>>> readExcelAllSheetMap = UtilExcel.readExcelAllSheetMap(file.getAbsolutePath()); | |
| 31 | + System.out.println("12"); | |
| 32 | + } catch (Exception e) { | |
| 33 | + e.printStackTrace(); | |
| 34 | + } | |
| 22 | 35 | } |
| 23 | 36 | |
| 24 | 37 | public static final String SEPARATE_CONSIGNEE_MOBILE = "__"; | ... | ... |