![](/img/trans.png)
[英]how to merge columns while exporting from jtable to excel? using apache POI
[英]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); // 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"));
所以結果是整個表的千位分隔符和兩位小數。 有沒有辦法只在特定列之后設置格式?
這是我的解決方案:
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.