0

こんにちはIamは、データ形式の文字列が「$」#、## 0として返される特定の形式でExcelのセルを解析しています。そのセル内の値を取得したいのですが、取得できません。誰か助けてもらえますか?

現在、このコードでは空白として値を取得していますが、Excelでは値はたとえば$40です。

 for (Row row : sheet) 
        {     
                Cell dataCell = row.getCell(colIndex); 

                if(dataCell!=null )
                {
                   if( row.getRowNum()==5)
                   {

                       System.out.println("Cell Value is::"+dataCell.toString());
                   }


                }

            }
4

3 に答える 3

1

文字列を、コーディングを行わずに、Excelと同じように魔法のようにフォーマットする必要があるようです。POIでそれを行うことができますが、それは通常のユースケースではありません。人々は通常、数値やブール値などの値を取得し、それらを自分で処理したいと考えています。

探しているクラスはDataFormatterです。あなたのコードは次のようになります

 DataFormatter fmt = new DataFormatter();
 for (Row r : sheet) {
    for (Cell c : r) {
       CellReference cr = new CellRefence(c);
       System.out.println("Cell " + cr.formatAsString() + " is " + 
                          fmt.formatCellValue(c) );
    }
 }
于 2012-07-26T20:18:45.527 に答える
0

「文字列型」を行うだけでなく、データ型に従って値を取得する必要があります。

HSSF の例:

HSSFCell cell = poiFilaActual.getCell(intColActual);
if (cell != null) {
    if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
        return cell.getRichStringCellValue().toString();
    }  else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
        return new String( (cell.getBooleanCellValue() == true ? "true" : "false") );
    } else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
        return "";
    } else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        if(HSSFDateUtil.isCellDateFormatted(cell)){
            return ( new SimpleDateFormat("dd/MM/yyyy").format(cell.getDateCellValue()) );
        }else{
            return new BigDecimal(cell.getNumericCellValue()).toString();
        }
    }
}
于 2012-07-26T19:32:32.213 に答える
0

セルの値を取得するには、このメソッドを使用します。

private String getCellValue(Cell cell) {
    if (cell == null) {
        return null;
    }
    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        return cell.getNumericCellValue() + "";
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return cell.getBooleanCellValue() + "";
    }else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
        return cell.getStringCellValue();
    }else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
        return cell.getErrorCellValue() + "";
    } 
    else {
        return null;
    }
}
于 2012-08-22T06:45:25.087 に答える