Apache POI 之 初学实战篇 (四) --- 文本对齐

初学实战篇

文本的对齐方式

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("One");
    Row row = sheet.createRow(2);
    row.setHeightInPoints(30);

    createCell(wb, row, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
    createCell(wb, row, 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
    createCell(wb, row, 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
    createCell(wb, row, 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
    createCell(wb, row, 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
    createCell(wb, row, 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
    createCell(wb, row, 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);

    FileOutputStream out = new FileOutputStream("wb7.xlsx");
    wb.write(out);
    out.close();
}

    /**
     * 根据指定的对其方式创建一个单元格
     * @param workbook
     * @param row
     * @param column
     * @param halign
     * @param valign
     */
private static void createCell(Workbook workbook, Row row, int column, short halign, short valign) {
    Cell cell = row.createCell(column);
    cell.setCellValue("对其方式");

    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);

    cell.setCellStyle(cellStyle);
}

你可能感兴趣的:(poi)