I am using Apache POI 5.0.0 for write data to excel. I try to set cell value as double like that format '11.33'
First one, I try to write value as double and set cell type as NUMERIC then Excel shows it '11,33'.
Second one, I try to write value as String and set cell type as STRING then Excel shows it '11.33', when double click to cell, excel change value to date and shows 'Nov.33'
I am using generic method for writing values to excel. First one is
else if (fieldClass == Double.class || fieldClass == double.class) {
double value = (double)field.get(obj);
cell.setCellValue(value);
cell.setCellType(CellType.NUMERIC);}
Second one is
else if (fieldClass == Double.class || fieldClass == double.class) {
double value = (double)field.get(obj);
cell.setCellValue(value "");
cell.setCellType(CellType.STRING);
}
CodePudding user response:
If you want to 2 decimal letters in excel you can use below code.
style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
This will make the value 0 show up as 0.00, 11.33 as 11.33 and 12.4 as 12.40
CodePudding user response:
You can use number formatter
NumberFormat numberFormat = new DecimalFormat("#0.00");
cell.setCellValue(numberFormat.format(value));
