Commit 60ed22eeed01325a26a56c4e9dc848006410ac7f
1 parent
299ab778
Exists in
master
获取自定义日期格式
Showing
2 changed files
with
13 additions
and
3 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; |
... | ... | @@ -15,6 +16,7 @@ import org.apache.poi.hssf.util.HSSFColor; |
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.Row; |
19 | 21 | import org.apache.poi.ss.usermodel.Sheet; |
20 | 22 | import org.apache.poi.ss.usermodel.Workbook; |
... | ... | @@ -331,7 +333,15 @@ public class UtilExcel { |
331 | 333 | if(currCellType.compareTo(CellType.STRING) == 0){ |
332 | 334 | return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); |
333 | 335 | }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ |
334 | - return df.format(cell.getNumericCellValue()); | |
336 | + | |
337 | + //if (DateUtil.isCellDateFormatted(cell)) { | |
338 | + if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) { | |
339 | + // 如果是date类型则 ,获取该cell的date值 | |
340 | + return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); | |
341 | + } else { // 纯数字 | |
342 | + // return String.valueOf(cell.getNumericCellValue()); | |
343 | + return df.format(cell.getNumericCellValue()); | |
344 | + } | |
335 | 345 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
336 | 346 | return cell.getBooleanCellValue(); |
337 | 347 | }else if(currCellType.compareTo(CellType.FORMULA) == 0){ |
... | ... | @@ -413,7 +423,7 @@ public class UtilExcel { |
413 | 423 | |
414 | 424 | public static void main(String args[]){ |
415 | 425 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
416 | - String filepath = "C:\\Users\\EDZ\\Desktop\\榴莲1 (5).xlsx"; | |
426 | + String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xlsx"; | |
417 | 427 | List<List<Object>> data = null; |
418 | 428 | try { |
419 | 429 | data = UtilExcel.readExcel(filepath); | ... | ... |