英文:
How to skip certain columns when setting formatting style while exporting to excel using Apache poi?
问题
我有一个包含12列的表格,我想在第4列开始设置千位分隔符和两位小数。这是我如何在表格中设置值的方式:
Cell cell = null;
for (int i = 0; i < tableView.getItems().size(); i++) {
HSSFRow hssfRow = hssfSheet.createRow(i + 3); // 跳过标题行
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());
}
}
}
然后我像这样设置千位分隔符和两位小数:
cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));
这样整个表格都会有千位分隔符和两位小数。是否有一种方法只在特定列之后设置格式化?
英文:
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?
答案1
得分: 0
这是我的解决方案:
DataFormat format = hssfWorkbook.createDataFormat();
HSSFCellStyle styleForTotal2 = hssfWorkbook.createCellStyle();
styleForTotal2.setDataFormat(format.getFormat("#,##0.00"));
// 循环部分:
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());
}
}
英文:
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());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论