我如何通过Apache POI在Excel中设置单元格样式?
我使用Apache POI的3.9版本来生成excel,这个代码是正确的:
public CellStyle getCellStyle(XSSFWorkbook workbook){ CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("###,###")); XSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 11); font.setFontName("Tahoma"); cellStyle.setFont(font); CellStyle style = workbook.createCellStyle(); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); return cellStyle; }
但升级到3.17时这些行有错误:
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
根据版本3.17发行说明在这里 ,它说“将单元格alignment常量从CellStyle迁移到HorizontalAlignment和VerticalAlignment枚举”使用下面的代码
style.setVerticalAlignment(VerticalAlignment.TOP); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN);