使用 Apache poi 导出到 excel 时设置格式样式时如何跳过某些列?

How to skip certain columns when setting formatting style while exporting to excel using Apache poi?

我有 12 列的 table,我想从第 4 列开始设置千位分隔符和两位小数。这就是我在 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());
        }
    }
}      

然后我像这样设置千位分隔符和两位小数:

cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));

所以结果是千位分隔符和两位小数table。有没有办法只在特定列之后设置格式?

这是我的解决方案:

DataFormat format = hssfWorkbook.createDataFormat();
HSSFCellStyle styleForTotal2 = hssfWorkbook.createCellStyle();
styleForTotal2.setDataFormat(format.getFormat("#,##0.00"));

上面的格式工作正常,它只格式化我想要的列。

For 循环:

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());
    }
}