Cannot get a text value from a numeric cell “Poi”

前端 未结 11 2085
余生分开走
余生分开走 2020-12-07 22:17

I\'m trying to consume data from a spreadsheet in Excel, but always of this error, already tried formatting the worksheet to text and number and still the error persists.

11条回答
  •  生来不讨喜
    2020-12-07 23:08

        Cell cell = sheet.getRow(i).getCell(0);
        cell.setCellType ( Cell.CELL_TYPE_STRING );
        String j_username = cell.getStringCellValue();
    

    UPDATE

    Ok, as have been said in comments, despite this works it isn't correct method of retrieving data from an Excel's cell.

    According to the manual here:

    If what you want to do is get a String value for your numeric cell, stop!. This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.

    And according to the DataFormatter API

    DataFormatter contains methods for formatting the value stored in an Cell. This can be useful for reports and GUI presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN, percentages, decimals, dates, phone numbers, zip codes, etc.

    So, right way to show numeric cell's value is as following:

     DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
     Cell cell = sheet.getRow(i).getCell(0);
     String j_username = formatter.formatCellValue(cell); //Returns the formatted value of a cell as a String regardless of the cell type.
    

提交回复
热议问题