有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想得到excel中显示的日期,比如日期类型单元格中的2014年12月1日

下面是我用来获取excel中显示的日期的代码,如:12/1/2014。单元格类型为日期类型。字符串dateFmt=cell。getCellStyle()。getDataFormatString()//返回空值

试试看{

    String excelFilePath = "D:\\readExcel\\QA 3715 nigbo PFL F1525W985.xls";       
    FileInputStream fileInputStream = new FileInputStream(excelFilePath);
    HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);      
    Sheet firstSheet = workbook.getSheetAt(0);

    Row row  = firstSheet.getRow(6);       
    Iterator<Cell> cellIterator = row.cellIterator();

    while (cellIterator.hasNext()) {

        Cell cell = cellIterator.next();
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            System.out.println(Cell.CELL_TYPE_STRING + " : "+ cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            System.out.println(Cell.CELL_TYPE_BOOLEAN+" : " + cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            System.out.println(Cell.CELL_TYPE_NUMERIC+" :: "+cell.getDateCellValue());// getting Mon Dec 01 00:00:00 IST 2014


            if(DateUtil.isCellDateFormatted(cell)){//returning false

                System.out.println("true.... : "+cell.getDateCellValue());

            }

          if (HSSFDateUtil.isCellDateFormatted(cell)){//returning false
              System.out.println("true.... : "+cell.getDateCellValue());
          }

            double dv = cell.getNumericCellValue();
            Date date = (Date) HSSFDateUtil.getJavaDate(dv);
            String dateFmt = cell.getCellStyle().getDataFormatString();
            String strValue = new CellDateFormatter(dateFmt).format(date);// getting null
            System.out.println("strValue : "+strValue);

             DataFormatter df = new DataFormatter();
            String cellValue = df.formatCellValue(cell);
            System.out.println("cellValue : "+cellValue);

        }       
    } 

    } catch (Exception e) {
        e.printStackTrace();
    }

我不想在外部应用任何日期格式


共 (0) 个答案