使用Apache POI以特定格式读取excel的“ Formula Date单元格”值excel具有STRING FORMULA和INT FORMULA类型

问题描述

作为Selenium自动化的一部分,我们需要读取Excel(Apache POI和xls格式文件hssf文件,其中包含STRING FORMULA和INTEGER FORMULA单元格,日期值和NUMERIC以及STRING单元格。

尝试以下方法时遇到的问题是DateFormat需要更新为(yyyy-mm-dd),但是其格式为dd / mm / yy。另外,NUMERIC值也显示为STRING。

我们定义了一个返回String的方法,以便可以读取该值并将其传递给JSON Payload。

public static String getData(String strSheetName,String strColumnIdentifier,int strRowIdentifier) throws IOException {

            String strAbsFilePath = getAbsolutePath("testdata" + File.separator + "New.xls");
            
            
            FileInputStream fis = new FileInputStream(strAbsFilePath);
    
        
            workbook = new hssfWorkbook(fis);
            fis.close();
            sheet = workbook.getSheet(strSheetName);
                            
            row = sheet.getRow(3);//// Making the object of excel row 
            
            DataFormatter formatter = new DataFormatter();
                
            
            int col_Num = -1;
            for (int i = 0; i < row.getLastCellNum(); i++) {
                if (row.getCell(i).getStringCellValue().trim().equals(strColumnIdentifier.trim()))
            
                    col_Num = i;
            }
            if (col_Num == -1)
                return "";
            row = sheet.getRow(strRowIdentifier);
            if (row == null)
                return "";
            
            cell = row.getCell(col_Num);
            formatter.formatCellValue(cell);
            
                            
            if(cell.getCellType() == CellType.FORMULA) {
                 PrintStream text = null;
                 switch(cell.getCachedFormulaResultType()) {
                  case STRING:
                     hssfRichTextString str = cell.getRichStringCellValue();
                     if(str != null && str.length() > 0) {
                        
                        String cellValue = str.toString();
                        return cellValue;
                     }
                     break;
                  case NUMERIC:
                         hssfCellStyle style = cell.getCellStyle();
                        
                         if(style == null) {
                            text.append( (char) cell.getNumericCellValue() );
                         } else {
                             
                            

    // cell.setCellType(CellType.NUMERIC);
                                 short df = workbook.createDataFormat().getFormat("dd/mm/yyy");
//                               style.setDataFormat(df);
                                // cell.setCellStyle(style);
                                 DateFormat datetemp = new SimpleDateFormat("dd/mm/yyy");
                                 Date date = cell.getDateCellValue();
                                    String cellValue1 = datetemp.format(date);
                                    //style.setDataFormat(df);//If used this,all Numeric cell values are set to date format
                                 cellValue1=formatter.formatRawCellContents(
                                        cell.getNumericCellValue(),style.getDataFormat(),style.getDataFormatString()
                                        
                                  );
                                return cellValue1;
                             }
                             break;
                      case BOOLEAN:   
                          cell.getBooleanCellValue();
                          
                             
                     }
                     
                     }
                String cellStringValue = formatter.formatCellValue(cell);
                return cellStringValue;
    }

输出

stringFieldValue value is: TestingExcelCell //Correctly getting as String
IntFieldValue value is: 11 //It is going as String in the Payload
ReportDate value is: 6/7/19 //This should come in the format of yyyy-mm-dd

请建议是否需要获取yyyy-mm-dd而不是dd / mm / yy中的DateFormat,并将Integer值设置为Integer而不是String(11)。

解决方法

问题是检查单元格是否为日期格式,然后在对条件进行以下更改之后设置日期格式,从而能够以预期格式获取值。 感谢所有支持。

  case NUMERIC:
                       //  HSSFCellStyle style = cell.getCellStyle();
                       CellStyle style = cell.getCellStyle();
                         if(style == null) {
                             cell.getNumericCellValue();
                            //text.append( (char) cell.getNumericCellValue() );
                         } else if(DateUtil.isCellDateFormatted(cell)) {
                             cell.setCellStyle(style);
                             SimpleDateFormat datetemp = new SimpleDateFormat("yyyy-MM-dd");
                             //String formattedCell = datetemp.format(cell.getDateCellValue());

                             Date date = cell.getDateCellValue();
                                String formattedCell = datetemp.format(date);
                                return formattedCell;
                         }