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