Apache poi中心对齐

8 java apache-poi

我想对齐文字.但是,文本未对齐.

Cell lastCell = lastCell = row.createCell(cellNumber++);
if (value != null) {
    lastCell.setCellValue(value);
}
CellStyle cellStyle = lastCell.getCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
lastCell.setCellStyle(cellStyle);
Run Code Online (Sandbox Code Playgroud)

小智 9

您可以替换:

cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
Run Code Online (Sandbox Code Playgroud)

与:

cellStyle.setAlignment(HorizontalAlignment.RIGHT);
Run Code Online (Sandbox Code Playgroud)


小智 7

    lastCell = row.createCell(cellNumber++);

    CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
    cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
    lastCell.setCellStyle(cellStyle);

    if (number != null) {
        lastCell.setCellValue(number);
    }
Run Code Online (Sandbox Code Playgroud)

从工作簿创建新的单元格样式.

    CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
Run Code Online (Sandbox Code Playgroud)