Search in sources :

Example 6 with CTTableCell

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell in project poi by apache.

the class TestXSLFTable method testCreate.

@Test
public void testCreate() throws IOException {
    XMLSlideShow ppt1 = new XMLSlideShow();
    XSLFSlide slide = ppt1.createSlide();
    XSLFTable tbl = slide.createTable();
    assertNotNull(tbl.getCTTable());
    assertNotNull(tbl.getCTTable().getTblGrid());
    assertNotNull(tbl.getCTTable().getTblPr());
    assertTrue(tbl.getXmlObject() instanceof CTGraphicalObjectFrame);
    assertEquals("Table 1", tbl.getShapeName());
    assertEquals(2, tbl.getShapeId());
    assertEquals(0, tbl.getRows().size());
    assertEquals(0, tbl.getCTTable().sizeOfTrArray());
    assertEquals(0, tbl.getCTTable().getTblGrid().sizeOfGridColArray());
    assertEquals(0, tbl.getNumberOfColumns());
    assertEquals(0, tbl.getNumberOfRows());
    XSLFTableRow row0 = tbl.addRow();
    assertNotNull(row0.getXmlObject());
    assertEquals(1, tbl.getNumberOfRows());
    assertSame(row0, tbl.getRows().get(0));
    assertEquals(20.0, row0.getHeight(), 0);
    row0.setHeight(30.0);
    assertEquals(30.0, row0.getHeight(), 0);
    assertEquals(0, row0.getCells().size());
    XSLFTableCell cell0 = row0.addCell();
    assertNotNull(cell0.getXmlObject());
    // by default table cell has no borders
    CTTableCell tc = (CTTableCell) cell0.getXmlObject();
    assertTrue(tc.getTcPr().getLnB().isSetNoFill());
    assertTrue(tc.getTcPr().getLnT().isSetNoFill());
    assertTrue(tc.getTcPr().getLnL().isSetNoFill());
    assertTrue(tc.getTcPr().getLnR().isSetNoFill());
    assertSame(cell0, row0.getCells().get(0));
    assertEquals(1, tbl.getNumberOfColumns());
    assertEquals(100.0, tbl.getColumnWidth(0), 0);
    cell0.addNewTextParagraph().addNewTextRun().setText("POI");
    assertEquals("POI", cell0.getText());
    XSLFTableCell cell1 = row0.addCell();
    assertSame(cell1, row0.getCells().get(1));
    assertEquals(2, tbl.getNumberOfColumns());
    assertEquals(100.0, tbl.getColumnWidth(1), 0);
    cell1.addNewTextParagraph().addNewTextRun().setText("Apache");
    assertEquals("Apache", cell1.getText());
    for (BorderEdge edge : BorderEdge.values()) {
        assertNull(cell1.getBorderWidth(edge));
        cell1.setBorderWidth(edge, 2.0);
        assertEquals(2.0, cell1.getBorderWidth(edge), 0);
        assertNull(cell1.getBorderColor(edge));
        cell1.setBorderColor(edge, Color.yellow);
        assertEquals(Color.yellow, cell1.getBorderColor(edge));
    }
    assertEquals(VerticalAlignment.TOP, cell1.getVerticalAlignment());
    cell1.setVerticalAlignment(VerticalAlignment.MIDDLE);
    assertEquals(VerticalAlignment.MIDDLE, cell1.getVerticalAlignment());
    cell1.setVerticalAlignment(null);
    assertEquals(VerticalAlignment.TOP, cell1.getVerticalAlignment());
    XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt1);
    ppt1.close();
    slide = ppt2.getSlides().get(0);
    tbl = (XSLFTable) slide.getShapes().get(0);
    assertEquals(2, tbl.getNumberOfColumns());
    assertEquals(1, tbl.getNumberOfRows());
    assertEquals("POI", tbl.getCell(0, 0).getText());
    assertEquals("Apache", tbl.getCell(0, 1).getText());
    ppt2.close();
}
Also used : CTGraphicalObjectFrame(org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame) CTTableCell(org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell) BorderEdge(org.apache.poi.sl.usermodel.TableCell.BorderEdge) Test(org.junit.Test)

Aggregations

CTTableCell (org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell)6 Test (org.junit.Test)2 BorderEdge (org.apache.poi.sl.usermodel.TableCell.BorderEdge)1 CTTableCellProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTTableCellProperties)1 CTTextBody (org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody)1 CTGraphicalObjectFrame (org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame)1