Hi all,
Yes I know ...this issue has been discussed over and over again. Ive read
the old posts and googled but could not find a solution. So I post my
question here.
My requirement is to read all cells in String format(with or without
formula).So when the cell type is String POI does its job effectively. When
POI returns me a numeric type, I try to read the formatting and reconstruct
the String. If this format is supported by Excel everything is fine. The
problem arises from a user-defined format(for example , try to apply Pi *
2). Excel returns me a cell with dataFormat : 0 and the cell value as
3.141592653589793.
After formatting the value i get is 3.
How do I use java DecimalFormat to format this String?? Here is my code :
public String getStringfromNumericValue(HSSFCell cell) {
short dataFormatNr = cell.getCellStyle().getDataFormat();
double numericVal = cell.getNumericCellValue();
String formattedString = null;
DecimalFormat df = null;
//checking if format is available in POI built in formats
if (dataFormatNr > 0 && dataFormatNr <
HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) {
df = new
DecimalFormat(HSSFDataFormat.getBuiltinFormat(dataFormatNr));
}
// if format not found
else {
df = new DecimalFormat();
String format =
this.getHSSFWorkbook().createDataFormat().getFormat(dataFormatNr);
if (format.toUpperCase().equals("GENERAL")) {
// I need help here for the format I set it to " " so that I can read
normal numbers without any formatting
format = " ";
}
df.applyPattern(format);
}
formattedString = df.format(numericVal);
}
thx you
Janap
--
View this message in context:
http://www.nabble.com/Formatting-numeric-cells-returning-%22GENERAL%22-%28dataFormat%3A-0%29-tf4152246.html#a11812457
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]