Commit 6db05b0940aadcc027f03b99f52dcb8a512c8c6d
1 parent
0d53168f
Exists in
master
优化excel读日期
Showing
2 changed files
with
5 additions
and
6 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilExcel.java
| ... | ... | @@ -10,11 +10,13 @@ import java.util.HashMap; |
| 10 | 10 | import java.util.List; |
| 11 | 11 | import java.util.Map; |
| 12 | 12 | |
| 13 | +import org.apache.poi.hssf.usermodel.HSSFDateUtil; | |
| 13 | 14 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 14 | 15 | import org.apache.poi.openxml4j.util.ZipSecureFile; |
| 15 | 16 | import org.apache.poi.ss.usermodel.Cell; |
| 16 | 17 | import org.apache.poi.ss.usermodel.CellStyle; |
| 17 | 18 | import org.apache.poi.ss.usermodel.CellType; |
| 19 | +import org.apache.poi.ss.usermodel.DateUtil; | |
| 18 | 20 | import org.apache.poi.ss.usermodel.FillPatternType; |
| 19 | 21 | import org.apache.poi.ss.usermodel.Row; |
| 20 | 22 | import org.apache.poi.ss.usermodel.Sheet; |
| ... | ... | @@ -449,10 +451,7 @@ public class UtilExcel { |
| 449 | 451 | */ |
| 450 | 452 | System.out.println(cell.getCellStyle().getDataFormat()); |
| 451 | 453 | short format = cell.getCellStyle().getDataFormat(); |
| 452 | - if (format==28 || format==22||format==31 || format == 58 || format==14 || format==57 || format==32 || format==20 || format==176 ) { | |
| 453 | - | |
| 454 | - // 如果是date类型则 ,获取该cell的date值 | |
| 455 | - //return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); | |
| 454 | + if (DateUtil.isCellDateFormatted(cell)) { | |
| 456 | 455 | SimpleDateFormat sdf = null; |
| 457 | 456 | if(format == 14 || format == 31 || format == 57 || format == 58 ){ |
| 458 | 457 | //日期 |
| ... | ... | @@ -513,7 +512,7 @@ public class UtilExcel { |
| 513 | 512 | System.out.println(df.format(3.3300)); |
| 514 | 513 | |
| 515 | 514 | |
| 516 | - String filepath = "C:\\Users\\EDZ\\Desktop\\ttt.xls"; | |
| 515 | + String filepath = "C:\\Users\\EDZ\\Desktop\\date - 副本.xlsx"; | |
| 517 | 516 | List<List<Object>> data = null; |
| 518 | 517 | |
| 519 | 518 | try { | ... | ... |