Search in sources :

Example 1 with CTRow

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow 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)

Example 2 with CTRow

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

the class XWPFFootnote method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     * @see org.apache.poi.xwpf.usermodel.IBody#getTableCell(CTTc cell)
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    if (tableRow == null) {
        return null;
    }
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 3 with CTRow

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

the class XWPFHeaderFooter method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        cursor.dispose();
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 4 with CTRow

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

the class XWPFTable method insertNewTableRow.

/**
     * inserts a new tablerow
     *
     * @param pos
     * @return the inserted row
     */
public XWPFTableRow insertNewTableRow(int pos) {
    if (pos >= 0 && pos <= tableRows.size()) {
        CTRow row = ctTbl.insertNewTr(pos);
        XWPFTableRow tableRow = new XWPFTableRow(row, this);
        tableRows.add(pos, tableRow);
        return tableRow;
    }
    return null;
}
Also used : CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)

Example 5 with CTRow

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

the class XWPFTableCell method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tr = table.getRow(row);
    if (tr == null) {
        return null;
    }
    return tr.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)6 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)5 XmlCursor (org.apache.xmlbeans.XmlCursor)3 XmlObject (org.apache.xmlbeans.XmlObject)3 CTRow (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow)3 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 Test (org.junit.Test)2 CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)2 ArrayList (java.util.ArrayList)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)1 Row (org.apache.poi.ss.usermodel.Row)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 CTSheetData (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData)1 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)1 CTRowImpl (org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl)1 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)1 CTR (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR)1 CTTc (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc)1