0

I am triing to read exact string values from an excel sheet using the POI library. The problem is that, it is not acceptable to require the user to set the cell type explicitly to text, so excel automatically converts numeric input into a numeric value.

When I'm triing to get the value back as a string, the following problem occurs:

  1. I'm triing to use the Cell.getStringValue() function. It throws an exception, since my field is implicitly set to a numeric type field.

  2. I'm checking for the field type, and if I notice this happens to be a numeric field, I'm converting it into a string with either Double.toString, or Cell.toString(), my field is converted by using decimals, even if it was a round value. For example I write the number '1' into the field. It automatically converts to the 1 numberic value. When I try to get back the string value, I get back the "1.0" string value, wich is problematic.

Is there a way to get back the original string in this scenario? The only solution I can come up after triing this is to forbid using decimals in the user input.

Robert
  • 1,658
  • 16
  • 26

1 Answers1

0

I think ms excel is lossy in this regard. For instance if you type 00123 in a cell it will automatically be converted to 123. This happens before reading the data with POI so unfortunately POI doesn't have access to the original value.

I think the only option is to set the cell formatting to string prior to entering data into the cell.

Very annoying!

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Very annoying indeed. It wouldn't be a problem, if the user could see the modification excel does, but since excel trims the shown value back to '1', it's bad. Maybe programming the trimming to be "excel-like" could work, but that would take too much effort right now. – Robert Oct 26 '15 at 11:54
  • 1
    Ah, if you just want to format the cell like excel does see [getCellStyle](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getCellStyle()) and [DataFormatter](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html) – lance-java Oct 26 '15 at 11:59
  • Well, it's an acceptable workaround :) The trimming is pretty trivial too, I'm not really experienced with java utility libraries, so it didn't pop to my mind immediately. Would you update your answer with this workaround? – Robert Oct 26 '15 at 12:01