如何在使用Apache POI导出到Excel时设置格式样式时跳过特定列?

huangapple go评论74阅读模式
英文:

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 &lt; tableView.getItems().size(); i++) {
    HSSFRow hssfRow = hssfSheet.createRow(i + 3); // skipping title                                 
    for (int col = 0; col &lt; 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(&quot;#,##0.00&quot;));

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(&quot;#,##0.00&quot;));

Format above is working fine, it only format columns that i want.

For loop:

for (int col = 4; col &lt; 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());
    }
}

huangapple
  • 本文由 发表于 2020年9月22日 16:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定