I need to write a Double value in a numeric cell using a specific format, i mean, the generated xls must have numeric cells containing Double values like, for example: 8,1. I am trying something like:
DecimalFormat dFormat = new DecimalFormat("##.#");
dFormat.format(doubleValue);
But, since format method returns a String, no matter whether I create cells as numeric or not, they always behave as text cells. I was thinking about two options:
Any idea?
Class DataFormatter. DataFormatter contains methods for formatting the value stored in a Cell. This can be useful for reports and GUI presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN, percentages, decimals, dates, phone numbers, zip codes, etc.
To create date cell which display current date to the cell, we can use Apache POI's style feature to format the cell according to the date. The setDateformat() method is used to set date format. Lets see an example in which we are creating a cell containing current date.
I may be missing something, but I think you just want to style the cell with a format rule to display a single decimal place
// Do this only once per file
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
wb.getCreationHelper().createDataFormat().getFormat("#.#"));
// Create the cell
Cell c = row.createCell(2);
c.setCellValue(8.1);
c.setCellStyle(cellStyle);
That will create a formatting rule, create a cell, set the cell to be the value 8.1, then style it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With