0

I am taking phone number from excel, using Apache POI.

System.out.println(
    "---------->" + cell.getNumericCellValue() + "--" + (int) cell.getNumericCellValue()
);

sel.setContactno(String.valueOf(cell.getNumericCellValue()));

Output:

---------->8.902305623E9--2147483647

Exception is

java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

How can I get the phone number properly?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • 4
    why would a phone number be a numeric cell value? – Scary Wombat Apr 23 '18 at 06:47
  • 2
    The code you've posted is using `getNumericCellValue`. The exception suggests you're using `getStringCellValue` – Michael Apr 23 '18 at 06:49
  • 1
    Formatter might be a good place to start with excel file: take a look at this thread: [https://stackoverflow.com/questions/30125465/cannot-get-a-text-value-from-a-numeric-cell-poi](https://stackoverflow.com/questions/30125465/cannot-get-a-text-value-from-a-numeric-cell-poi) – Michael Ambaye Apr 23 '18 at 06:52

1 Answers1

3

java.lang.IllegalStateException - if the cell type returned by getCellType() is CellType.STRING

Most likely cell.getNumericCellValue() seems not to contain double. Please verify your excel data.

Edit: I think the phone number should not be just a single entity in the first place. It can be composed of local code, country code etc. which can contain special characters like +. And thus it definitely should not be numeric. This composition should provide the desired flexibility. But it completely depends on your/op requirements.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
  • Would it be generally more flexible or better practice to get the phone number as a String? Given that they often contain non-numeric characters such as `( ) -`, it would seem to me to be the more robust option. – Austin Schaefer Apr 23 '18 at 09:17
  • 1
    @AustinSchäfer I think the phone number should not be just a single entity in the first place. It can be composed of local code, coutry code etc. which can contain special characters like `+`. This composition should provide the desired flexibility. But it completely depends on the op's requirements. – Shanu Gupta Apr 23 '18 at 09:19
  • Makes sense to me. Thanks for the quick answer! :) – Austin Schaefer Apr 23 '18 at 09:20