Search in sources :

Example 1 with CTRow

use of org.openxmlformats.schemas.wordprocessingml.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 2 with CTRow

use of org.openxmlformats.schemas.wordprocessingml.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 3 with CTRow

use of org.openxmlformats.schemas.wordprocessingml.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 4 with CTRow

use of org.openxmlformats.schemas.wordprocessingml.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)

Example 5 with CTRow

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

the class TestXSSFSheet method createRow.

/**
     * Rows and cells can be created in random order,
     * but CTRows are kept in ascending order
     */
@Override
@Test
public void createRow() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet();
    CTWorksheet wsh = sheet.getCTWorksheet();
    CTSheetData sheetData = wsh.getSheetData();
    assertEquals(0, sheetData.sizeOfRowArray());
    XSSFRow row1 = sheet.createRow(2);
    row1.createCell(2);
    row1.createCell(1);
    XSSFRow row2 = sheet.createRow(1);
    row2.createCell(2);
    row2.createCell(1);
    row2.createCell(0);
    XSSFRow row3 = sheet.createRow(0);
    row3.createCell(3);
    row3.createCell(0);
    row3.createCell(2);
    row3.createCell(5);
    CTRow[] xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    assertTrue(xrow[0].equals(row3.getCTRow()));
    assertEquals(3, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertTrue(xrow[1].equals(row2.getCTRow()));
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    assertTrue(xrow[2].equals(row1.getCTRow()));
    CTCell[] xcell = xrow[0].getCArray();
    assertEquals("D1", xcell[0].getR());
    assertEquals("A1", xcell[1].getR());
    assertEquals("C1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    //re-creating a row does NOT add extra data to the parent
    row2 = sheet.createRow(1);
    assertEquals(3, sheetData.sizeOfRowArray());
    //existing cells are invalidated
    assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
    assertEquals(0, row2.getPhysicalNumberOfCells());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    wsh = sheet.getCTWorksheet();
    xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    //cells are now sorted
    xcell = xrow[0].getCArray();
    assertEquals("A1", xcell[0].getR());
    assertEquals("C1", xcell[1].getR());
    assertEquals("D1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    assertEquals(0, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    wb2.close();
}
Also used : CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) CTSheetData(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Test(org.junit.Test)

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 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)1 CTSheetData (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData)1 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)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 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)1