Commit 89401b411fd27b07dcfe19ae3f8b7a0e5000943a
Exists in
master
fix a bug
Showing
2 changed files
with
21 additions
and
19 deletions
Show diff stats
build.gradle
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,6 +11,7 @@ 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; |
14 | 16 | import org.apache.poi.openxml4j.util.ZipSecureFile; |
15 | 17 | import org.apache.poi.ss.usermodel.Cell; |
... | ... | @@ -387,10 +389,6 @@ public class UtilExcel { |
387 | 389 | } |
388 | 390 | Workbook wb = UtilExcel.getWorkbook(filepath, true); |
389 | 391 | Sheet sheet = wb.getSheetAt(sheetIndex); |
390 | - int end = sheet.getLastRowNum(); | |
391 | - if(end > UtilExcel.maxExcelRowNum){ | |
392 | - //throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); | |
393 | - } | |
394 | 392 | return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); |
395 | 393 | } |
396 | 394 | |
... | ... | @@ -400,11 +398,7 @@ public class UtilExcel { |
400 | 398 | |
401 | 399 | private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{ |
402 | 400 | Sheet sheet = wb.getSheetAt(sheetIndex); |
403 | - int end = sheet.getLastRowNum(); | |
404 | 401 | if(hasRowLimit){ |
405 | - if(end > UtilExcel.maxExcelRowNum) { | |
406 | - throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); | |
407 | - } | |
408 | 402 | return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); |
409 | 403 | }else { |
410 | 404 | return readExcelBySheet(sheet, Integer.MAX_VALUE, false); |
... | ... | @@ -459,13 +453,25 @@ public class UtilExcel { |
459 | 453 | if(currCellType.compareTo(CellType.STRING) == 0){ |
460 | 454 | return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); |
461 | 455 | }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ |
462 | - if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { | |
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 | + */ | |
465 | + System.out.println(cell.getCellStyle().getDataFormat()); | |
466 | + if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 | |
467 | + || cell.getCellStyle().getDataFormat() == 58 || cell.getCellStyle().getDataFormat()==14 | |
468 | + || cell.getCellStyle().getDataFormat()==57 || cell.getCellStyle().getDataFormat()==32 || cell.getCellStyle().getDataFormat()==20) { | |
463 | 469 | // 如果是date类型则 ,获取该cell的date值 |
464 | 470 | return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); |
465 | - } else { | |
466 | - // 纯数字gaoming 新增兼容浮点型,转成字符串读取,不然小数点被省略了 | |
467 | - cell.setCellType(CellType.STRING); | |
468 | - return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
471 | + } else { | |
472 | + //gaoming 这个是poi自带获取value通用,如果再有问题就用这个试试!!! | |
473 | + HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); | |
474 | + return dataFormatter.formatCellValue(cell); | |
469 | 475 | } |
470 | 476 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
471 | 477 | return cell.getBooleanCellValue(); |
... | ... | @@ -497,16 +503,12 @@ public class UtilExcel { |
497 | 503 | } |
498 | 504 | Workbook wb = UtilExcel.getWorkbook(filepath, true); |
499 | 505 | Sheet sheet = wb.getSheetAt(sheetIndex); |
500 | - int end = sheet.getLastRowNum(); | |
501 | - if(end > UtilExcel.maxExcelRowNum){ | |
502 | - throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); | |
503 | - } | |
504 | 506 | return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, true); |
505 | 507 | } |
506 | 508 | |
507 | 509 | public static void main(String args[]){ |
508 | 510 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
509 | - String filepath = "C:\\Users\\gaoming\\Desktop\\mmm.xlsx"; | |
511 | + String filepath = "C:\\Users\\EDZ\\Desktop\\aaa.xlsx"; | |
510 | 512 | List<List<Object>> data = null; |
511 | 513 | |
512 | 514 | try { | ... | ... |