Issue
I have table with 12 columns and i want to set thousand separator and two decimals starting from column 4. This is how i set values in table:
Cell cell = null;
for (int i = 0; i < tableView.getItems().size(); i++) {
HSSFRow hssfRow = hssfSheet.createRow(i + 3); // skipping title
for (int col = 0; col < tableView.getColumns().size(); col++) {
Object celValue = tableView.getColumns().get(col).getCellObservableValue(i).getValue();
try {
if (celValue != null) {
cell = hssfRow.createCell(col);
cell.setCellValue(Double.parseDouble(celValue.toString()));
}
} catch (NumberFormatException e) {
hssfRow.createCell(col).setCellValue(celValue.toString());
}
}
}
Then I am setting thousand separator and two decimals like this:
cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
So the result is thousand separator and two decimals on whole table. Is there a way to set formatting only after certain column?
Solution
This is my solution:
DataFormat format = hssfWorkbook.createDataFormat();
HSSFCellStyle styleForTotal2 = hssfWorkbook.createCellStyle();
styleForTotal2.setDataFormat(format.getFormat("#,##0.00"));
Format above is working fine, it only format columns that i want.
For loop:
for (int col = 4; col < tableView.getColumns().size(); col++) {
Object celValue = tableView.getColumns().get(col).getCellObservableValue(i).getValue();
try {
if (celValue != null) {
Cell = hssfRow.createCell(col);
Cell.setCellValue(Double.parseDouble(celValue.toString()));
Cell.setCellStyle(styleForTotal2);
}
} catch (NumberFormatException e) {
hssfRow.createCell(col).setCellValue(celValue.toString());
}
}
Answered By - Nem Jov