Commit 45a966adb2052fc0f9cf88cc79e0268c7a880ac7
Exists in
master
Merge branch 'master' of gitlab.taover.com:taov-erp/com-taover-util
Showing
2 changed files
with
21 additions
and
4 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilExcel.java
... | ... | @@ -4,6 +4,7 @@ import java.io.File; |
4 | 4 | import java.io.FileInputStream; |
5 | 5 | import java.io.FileOutputStream; |
6 | 6 | import java.text.DecimalFormat; |
7 | +import java.text.SimpleDateFormat; | |
7 | 8 | import java.util.ArrayList; |
8 | 9 | import java.util.Date; |
9 | 10 | import java.util.HashMap; |
... | ... | @@ -14,6 +15,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
14 | 15 | import org.apache.poi.ss.usermodel.Cell; |
15 | 16 | import org.apache.poi.ss.usermodel.CellStyle; |
16 | 17 | import org.apache.poi.ss.usermodel.CellType; |
18 | +import org.apache.poi.ss.usermodel.DateUtil; | |
17 | 19 | import org.apache.poi.ss.usermodel.Row; |
18 | 20 | import org.apache.poi.ss.usermodel.Sheet; |
19 | 21 | import org.apache.poi.ss.usermodel.Workbook; |
... | ... | @@ -379,7 +381,15 @@ public class UtilExcel { |
379 | 381 | if(currCellType.compareTo(CellType.STRING) == 0){ |
380 | 382 | return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); |
381 | 383 | }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ |
382 | - return df.format(cell.getNumericCellValue()); | |
384 | + | |
385 | + //if (DateUtil.isCellDateFormatted(cell)) { | |
386 | + if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { | |
387 | + // 如果是date类型则 ,获取该cell的date值 | |
388 | + return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); | |
389 | + } else { // 纯数字 | |
390 | + // return String.valueOf(cell.getNumericCellValue()); | |
391 | + return df.format(cell.getNumericCellValue()); | |
392 | + } | |
383 | 393 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
384 | 394 | return cell.getBooleanCellValue(); |
385 | 395 | }else if(currCellType.compareTo(CellType.FORMULA) == 0){ |
... | ... | @@ -440,7 +450,14 @@ public class UtilExcel { |
440 | 450 | if(currCellType.compareTo(CellType.STRING) == 0){ |
441 | 451 | dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString())); |
442 | 452 | }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ |
443 | - dataRow.add(df.format(cell.getNumericCellValue())); | |
453 | + //if (DateUtil.isCellDateFormatted(cell)) { | |
454 | + if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { | |
455 | + // 如果是date类型则 ,获取该cell的date值 | |
456 | + dataRow.add( new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue()))); | |
457 | + } else { // 纯数字 | |
458 | + // return String.valueOf(cell.getNumericCellValue()); | |
459 | + dataRow.add( df.format(cell.getNumericCellValue())); | |
460 | + } | |
444 | 461 | |
445 | 462 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
446 | 463 | dataRow.add(cell.getBooleanCellValue()); |
... | ... | @@ -509,7 +526,7 @@ public class UtilExcel { |
509 | 526 | |
510 | 527 | public static void main(String args[]){ |
511 | 528 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
512 | - String filepath = "C:\\Users\\EDZ\\Desktop\\榴莲1 (5).xlsx"; | |
529 | + String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xlsx"; | |
513 | 530 | List<List<Object>> data = null; |
514 | 531 | try { |
515 | 532 | data = UtilExcel.readExcel(filepath); | ... | ... |