Search in sources :

Example 6 with CTRow

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

Example 7 with CTRow

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

the class TestXSSFSheet method groupUngroupRow.

@Test
public void groupUngroupRow() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    //one level
    sheet.groupRow(9, 10);
    assertEquals(2, sheet.getPhysicalNumberOfRows());
    CTRow ctrow = sheet.getRow(9).getCTRow();
    assertNotNull(ctrow);
    assertEquals(10, ctrow.getR());
    assertEquals(1, ctrow.getOutlineLevel());
    assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
    //two level
    sheet.groupRow(10, 13);
    assertEquals(5, sheet.getPhysicalNumberOfRows());
    ctrow = sheet.getRow(10).getCTRow();
    assertNotNull(ctrow);
    assertEquals(11, ctrow.getR());
    assertEquals(2, ctrow.getOutlineLevel());
    assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
    sheet.ungroupRow(8, 10);
    assertEquals(4, sheet.getPhysicalNumberOfRows());
    assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
    sheet.ungroupRow(10, 10);
    assertEquals(3, sheet.getPhysicalNumberOfRows());
    assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
    workbook.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Test(org.junit.Test)

Example 8 with CTRow

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

the class XSSFRow method onDocumentWrite.

/**
     * Fired when the document is written to an output stream.
     *
     * @see org.apache.poi.xssf.usermodel.XSSFSheet#write(java.io.OutputStream) ()
     */
protected void onDocumentWrite() {
    // check if cells in the CTRow are ordered
    boolean isOrdered = true;
    CTCell[] cArray = _row.getCArray();
    if (cArray.length != _cells.size()) {
        isOrdered = false;
    } else {
        int i = 0;
        for (XSSFCell cell : _cells.values()) {
            CTCell c1 = cell.getCTCell();
            CTCell c2 = cArray[i++];
            String r1 = c1.getR();
            String r2 = c2.getR();
            if (!(r1 == null ? r2 == null : r1.equals(r2))) {
                isOrdered = false;
                break;
            }
        }
    }
    if (!isOrdered) {
        cArray = new CTCell[_cells.size()];
        int i = 0;
        for (XSSFCell xssfCell : _cells.values()) {
            cArray[i] = (CTCell) xssfCell.getCTCell().copy();
            // we have to copy and re-create the XSSFCell here because the 
            // elements as otherwise setCArray below invalidates all the columns!
            // see Bug 56170, XMLBeans seems to always release previous objects
            // in the CArray, so we need to provide completely new ones here!
            //_cells.put(entry.getKey(), new XSSFCell(this, cArray[i]));
            xssfCell.setCTCell(cArray[i]);
            i++;
        }
        _row.setCArray(cArray);
    }
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)

Example 9 with CTRow

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

the class TestXWPFTable method testCreateRow.

public void testCreateRow() {
    XWPFDocument doc = new XWPFDocument();
    CTTbl table = CTTbl.Factory.newInstance();
    CTRow r1 = table.addNewTr();
    r1.addNewTc().addNewP();
    r1.addNewTc().addNewP();
    CTRow r2 = table.addNewTr();
    r2.addNewTc().addNewP();
    r2.addNewTc().addNewP();
    CTRow r3 = table.addNewTr();
    r3.addNewTc().addNewP();
    r3.addNewTc().addNewP();
    XWPFTable xtab = new XWPFTable(table, doc);
    assertEquals(3, xtab.getNumberOfRows());
    assertNotNull(xtab.getRow(2));
    //add a new row
    xtab.createRow();
    assertEquals(4, xtab.getNumberOfRows());
    //check number of cols
    assertEquals(2, table.getTrArray(0).sizeOfTcArray());
    //check creation of first row
    xtab = new XWPFTable(CTTbl.Factory.newInstance(), doc);
    assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)

Example 10 with CTRow

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

the class TestXWPFTable method testGetText.

public void testGetText() {
    XWPFDocument doc = new XWPFDocument();
    CTTbl table = CTTbl.Factory.newInstance();
    CTRow row = table.addNewTr();
    CTTc cell = row.addNewTc();
    CTP paragraph = cell.addNewP();
    CTR run = paragraph.addNewR();
    CTText text = run.addNewT();
    text.setStringValue("finally I can write!");
    XWPFTable xtab = new XWPFTable(table, doc);
    assertEquals("finally I can write!\n", xtab.getText());
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) CTTc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc)

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