Commit 78e23f0f677dd1ef0bc676b21b465b2223a25ca7

Authored by gaoming
2 parents 141c9ffe 9b42d9d7
Exists in master

优化excel

build.gradle
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.69'
  62 + version '1.1.90'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/UtilExcel.java
... ... @@ -2,6 +2,7 @@ package com.taover.util;
2 2  
3 3 import java.io.File;
4 4 import java.io.FileOutputStream;
  5 +import java.math.BigDecimal;
5 6 import java.text.DecimalFormat;
6 7 import java.text.SimpleDateFormat;
7 8 import java.util.ArrayList;
... ... @@ -10,7 +11,9 @@ import java.util.HashMap;
10 11 import java.util.List;
11 12 import java.util.Map;
12 13  
  14 +import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
13 15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16 +import org.apache.poi.openxml4j.util.ZipSecureFile;
14 17 import org.apache.poi.ss.usermodel.Cell;
15 18 import org.apache.poi.ss.usermodel.CellStyle;
16 19 import org.apache.poi.ss.usermodel.CellType;
... ... @@ -28,7 +31,11 @@ public class UtilExcel {
28 31 private final static String CSV =".csv"; //csv
29 32  
30 33 private final static int maxExcelRowNum = 10000;
31   - private final static int maxExcelColumnNum = 50;
  34 + private final static int maxExcelColumnNum = 100;
  35 +
  36 + static {
  37 + ZipSecureFile.setMinInflateRatio(-1.0d);
  38 + }
32 39  
33 40 /**
34 41 * 描述:根据文件后缀,自适应上传文件的版本
... ... @@ -382,10 +389,6 @@ public class UtilExcel {
382 389 }
383 390 Workbook wb = UtilExcel.getWorkbook(filepath, true);
384 391 Sheet sheet = wb.getSheetAt(sheetIndex);
385   - int end = sheet.getLastRowNum();
386   - if(end > UtilExcel.maxExcelRowNum){
387   - throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
388   - }
389 392 return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false);
390 393 }
391 394  
... ... @@ -395,11 +398,7 @@ public class UtilExcel {
395 398  
396 399 private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{
397 400 Sheet sheet = wb.getSheetAt(sheetIndex);
398   - int end = sheet.getLastRowNum();
399 401 if(hasRowLimit){
400   - if(end > UtilExcel.maxExcelRowNum) {
401   - throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
402   - }
403 402 return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false);
404 403 }else {
405 404 return readExcelBySheet(sheet, Integer.MAX_VALUE, false);
... ... @@ -454,14 +453,43 @@ public class UtilExcel {
454 453 if(currCellType.compareTo(CellType.STRING) == 0){
455 454 return UtilString.trimCodePage(cell.getRichStringCellValue().getString());
456 455 }else if(currCellType.compareTo(CellType.NUMERIC) == 0){
457   - if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) {
458   - // 如果是date类型则 ,获取该cell的date值
459   - return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue()));
460   - } else {
461   - // 纯数字gaoming 新增兼容浮点型,转成字符串读取,不然小数点被省略了
462   - cell.setCellType(CellType.STRING);
463   - return UtilString.trimCodePage(cell.getRichStringCellValue().getString());
  456 +
  457 + /**
  458 + * yyyy-MM-dd----- 14
  459 + yyyy年m月d日--- 31
  460 + yyyy年m月------- 57
  461 + m月d日 ---------- 58
  462 + HH:mm----------- 20
  463 + h时mm分 ------- 32
  464 + yyyy-MM-dd HH:hh:ss 22
  465 + */
  466 + System.out.println(cell.getCellStyle().getDataFormat());
  467 + short format = cell.getCellStyle().getDataFormat();
  468 + if (cell.getCellStyle().getDataFormat()==28 || cell.getCellStyle().getDataFormat()==22||cell.getCellStyle().getDataFormat()==31
  469 + || cell.getCellStyle().getDataFormat() == 58 || cell.getCellStyle().getDataFormat()==14
  470 + || cell.getCellStyle().getDataFormat()==57 || cell.getCellStyle().getDataFormat()==32 || cell.getCellStyle().getDataFormat()==20) {
  471 +
  472 + // 如果是date类型则 ,获取该cell的date值
  473 + //return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue()));
  474 + SimpleDateFormat sdf = null;
  475 + if(format == 14 || format == 31 || format == 57 || format == 58){
  476 + //日期
  477 + sdf = new SimpleDateFormat("yyyy-MM-dd");
  478 + }else if (format == 20 || format == 32) {
  479 + //时间
  480 + sdf = new SimpleDateFormat("HH:mm");
  481 + }else {
  482 + sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  483 + }
  484 + double value = cell.getNumericCellValue();
  485 + Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
  486 + return sdf.format(date);
  487 + } else {
  488 + //gaoming 这个是poi自带获取value通用,如果再有问题就用这个试试!!!此处用于获取纯数字和小数类型
  489 + HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
  490 + return dataFormatter.formatCellValue(cell);
464 491 }
  492 +
465 493 }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){
466 494 return cell.getBooleanCellValue();
467 495 }else if(currCellType.compareTo(CellType.FORMULA) == 0){
... ... @@ -492,16 +520,13 @@ public class UtilExcel {
492 520 }
493 521 Workbook wb = UtilExcel.getWorkbook(filepath, true);
494 522 Sheet sheet = wb.getSheetAt(sheetIndex);
495   - int end = sheet.getLastRowNum();
496   - if(end > UtilExcel.maxExcelRowNum){
497   - throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
498   - }
499 523 return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, true);
500 524 }
501 525  
502 526 public static void main(String args[]){
503 527 //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx";
504   - String filepath = "C:\\Users\\gaoming\\Desktop\\jvm_crash.xls";
  528 + String filepath = "C:\\Users\\gaoming\\Desktop\\测试返单数量.xlsx";
  529 + //String filepath = "C:\\Users\\EDZ\\Desktop\\aaa.xlsx";
505 530 List<List<Object>> data = null;
506 531  
507 532 try {
... ... @@ -513,10 +538,12 @@ public class UtilExcel {
513 538 System.out.println((end-start)/1000);
514 539  
515 540  
516   - System.out.println(map);
517   -// data = map.get("0");
518   -//
519   -// System.out.println(data);
  541 + //System.out.println(map);
  542 + data = map.get("0");
  543 + System.out.println(data);
  544 + for (int i = 0; i < data.size(); i++) {
  545 + System.out.println(data.get(i).get(1));
  546 + }
520 547 // System.out.println(data.size());
521 548 // System.out.println(UtilExcel.readExcelAllSheetMap(filepath));
522 549 } catch (Exception e) {
... ...
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
... ... @@ -9,6 +9,17 @@ import com.taover.util.UtilExcel;
9 9  
10 10 public class TempExcel {
11 11 public static void main(String[] args){
  12 + checkExcelError();
  13 + }
  14 +
  15 + public static void checkExcelError() {
  16 + List<List<Object>> data = null;
  17 + try {
  18 + data = UtilExcel.readExcelAllSheet("C:\\Users\\Administrator\\Desktop\\异常Excel\\大文件测试2.xlsx");
  19 + } catch (Exception e) {
  20 + e.printStackTrace();
  21 + }
  22 + System.out.println("end");
12 23 //dealExcel();
13 24 readExcel();
14 25 }
... ...