我正在使用primefaces 5.1,默认情况下,使用所有列导出excel作为文本。我试图通过后期处理来做到以下几点。
我正在使用后处理并迭代到单元格级别进行格式化。
public void postProcessXLS(Object document) {
HSSFWorkbook workbook = (HSSFWorkbook) document;
HSSFSheet sheet = workbook.getSheetAt(0);
columnFormat(workbook, sheet, 3);
}
private void columnFormat(HSSFWorkbook workbook, HSSFSheet sheet, int colNo) {
HSSFCellStyle numberStyle = workbook.createCellStyle();
numberStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator <Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
System.err.print(cell.getColumnIndex()+ "\t\t");
if (cell.getColumnIndex()==colNo) {
cell.setCellStyle(numberStyle);
//cell.setCellType(Cell.CELL_TYPE_NUMERIC); //2nd approach, did'nt work
}
}
}
在这两种情况下,该列仍然以文本格式导出。我是否在设置数字格式时出错?