简体   繁体   English

如何通过Apache POI在Excel中设置单元格的样式?

[英]how can I styling cells in Excel by Apache POI?

I used version 3.9 of Apache POI for generate excel,and this code is correct on this: 我使用3.9版的Apache POI生成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;
}

but when upgrade to 3.17و These lines have errors: 但是升级到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);

According to version 3.17 release note here , it says "migrate cell alignment constants from CellStyle to HorizontalAlignment and VerticalAlignment enums" use below code instead 根据此处的版本3.17发行说明,它表示“将单元格对齐常量从CellStyle迁移到Horizo​​ntalAlignment和VerticalAlignment枚举”使用下面的代码代替

style.setVerticalAlignment(VerticalAlignment.TOP);
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM