views:

74

answers:

2

hi I wanted to get the value of a Numeric cell as a simple string.

Suppose there the type of cell is numeric with value 90%. Now I cannot use cell.getStringCellValue() as it will throw exception. I also cannot use cell.getNumericCellValue() as it will return me .9 and not 90%.

I want to store in db which is of type varchar2, so i want the value in string only. I cannot change the cell type in xls as its the end user job, i have to handle this in code itself.

Also formatter does't work well as there could be differentcell types in the xls...dd:mm,dd:mm:ss,formula etc.

All i Want is that whatever the cell type is i need to get its value as simple String.

plz Help

Thanks for ur time

+1  A: 

Try

cell.getRichStringCellValue ().getString();    

Have a look at this example
Here is Doc

org.life.java
cell.getRichStringCellValue ().getString(); It Didn,t work...Error:-lang.NoSuchMethodError I am using poi 3.6 jar
Dhananjay
@user371238 Have you checked the example's code , Try with older version of jar. document shows the method and it is there.
org.life.java
+1  A: 

You can force the value to be returned as a String using the methods below

HSSFDataFormatter hdf = new HSSFDataFormatter();
System.out.println (hdf.formatCellValue(mycell));

will return "90%"

The API for this method is at http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue%28org.apache.poi.ss.usermodel.Cell%29

This works directly even with an HSSFCell

it worked for me even when my Cell is an HSSFCell

i've also tried this cast - which works.

HSSFCell cell1 = (HSSFCell) row1.getCell(2);

HSSFDataFormatter hdf = new HSSFDataFormatter();
System.out.println ("formatted "+ hdf.formatCellValue(cell1));
JoseK
HSSFDataFormatter hdf = new HSSFDataFormatter();System.out.println (hdf.formatCellValue(mycell));In my case the "myCell" argument is of type HSSFCell but hdf.formatCellValue(mycell) take "mycell" as a type of Cell. I tried typecasting it to Cell but it throws an exception:java.lang.ClassCastException.....actually my code is written using HSSF API...
Dhananjay
@user371238: see my update
JoseK
I tried with HSSF cell ...It works but when i run as a standalone java class with java 1.6....I have my application on portal 6.1 where i have to use java 1.5...It throws exception there..
Dhananjay
@user371238: I've tried this with standalone 1.5 and 1.6. do you have the latest poi libraries in server web-inf/lib?
JoseK
i am using poi 3.5 Final
Dhananjay
okay then try 3.6 which is what i'm using
JoseK
tried 3.6...still getting the same error=java.lang.IncompatibleClassChangeError
Dhananjay
can you post the whole stacktrace as an Edit to your original question?
JoseK
i can run on standalone java class with 1.5 also ..but its not running on portal server 6.1 which has 1.5...with 3.6 jar
Dhananjay
okay i dont do IBM or icefaces, so cant help you there.
JoseK