Search in sources :

Example 1 with CTRowImpl

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl in project poi by apache.

the class AligningCells method centerAcrossSelection.

/**
     * Center a text over multiple columns using ALIGN_CENTER_SELECTION
     *
     * @param wb the workbook
     * @param row the row to create the cell in
     * @param start_column  the column number to create the cell in and where the selection starts
     * @param end_column    the column number where the selection ends
     * @param valign the horizontal alignment for the cell.
     */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, int start_column, int end_column, VerticalAlignment valign) {
    CreationHelper ch = wb.getCreationHelper();
    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);
    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }
    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(ch.createRichTextString("Align It"));
    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();
    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;
    List<Object> spanList = new ArrayList<Object>();
    spanList.add(span);
    //add spns to the row
    ctRow.setSpans(spanList);
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) ArrayList(java.util.ArrayList) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTRowImpl(org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl)

Aggregations

ArrayList (java.util.ArrayList)1 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 CTRowImpl (org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl)1