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);
}
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);
}
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;
}
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);
}
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();
}
Aggregations