Commit 60ed22eeed01325a26a56c4e9dc848006410ac7f

Authored by gaoming
1 parent 299ab778
Exists in master

获取自定义日期格式

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.29'
  62 + version '1.1.30'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
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);
... ...