How can I read numeric strings in Excel cells as string (not numbers)?

joycollector picture joycollector · Jul 2, 2009 · Viewed 260.2k times · Source
  1. I have excel file with such contents:

    • A1: SomeString

    • A2: 2

    All fields are set to String format.

  2. When I read the file in java using POI, it tells that A2 is in numeric cell format.

  3. The problem is that the value in A2 can be 2 or 2.0 (and I want to be able to distinguish them) so I can't just use .toString().

What can I do to read the value as string?

Answer

wil picture wil · Aug 17, 2010

I had same problem. I did cell.setCellType(Cell.CELL_TYPE_STRING); before reading the string value, which solved the problem regardless of how the user formatted the cell.