Example #1
0
  public void testRead() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
    XSSFSheet sheet = wb.getSheetAt(0);
    // the sheet has one relationship and it is XSSFDrawing
    List<POIXMLDocumentPart> rels = sheet.getRelations();
    assertEquals(1, rels.size());
    assertTrue(rels.get(0) instanceof XSSFDrawing);

    XSSFDrawing drawing = (XSSFDrawing) rels.get(0);
    // sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
    assertSame(drawing, sheet.createDrawingPatriarch());
    String drawingId = drawing.getPackageRelationship().getId();

    // there should be a relation to this drawing in the worksheet
    assertTrue(sheet.getCTWorksheet().isSetDrawing());
    assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());

    List<XSSFShape> shapes = drawing.getShapes();
    assertEquals(6, shapes.size());

    assertTrue(shapes.get(0) instanceof XSSFPicture);
    assertTrue(shapes.get(1) instanceof XSSFPicture);
    assertTrue(shapes.get(2) instanceof XSSFPicture);
    assertTrue(shapes.get(3) instanceof XSSFPicture);
    assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
    assertTrue(shapes.get(5) instanceof XSSFPicture);

    for (XSSFShape sh : shapes) assertNotNull(sh.getAnchor());

    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  }
Example #2
0
  public void testClone() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
    XSSFSheet sheet1 = wb.getSheetAt(0);

    XSSFSheet sheet2 = wb.cloneSheet(0);

    // the source sheet has one relationship and it is XSSFDrawing
    List<POIXMLDocumentPart> rels1 = sheet1.getRelations();
    assertEquals(1, rels1.size());
    assertTrue(rels1.get(0) instanceof XSSFDrawing);

    List<POIXMLDocumentPart> rels2 = sheet2.getRelations();
    assertEquals(1, rels2.size());
    assertTrue(rels2.get(0) instanceof XSSFDrawing);

    XSSFDrawing drawing1 = (XSSFDrawing) rels1.get(0);
    XSSFDrawing drawing2 = (XSSFDrawing) rels2.get(0);
    assertNotSame(drawing1, drawing2); // drawing2 is a clone of drawing1

    List<XSSFShape> shapes1 = drawing1.getShapes();
    List<XSSFShape> shapes2 = drawing2.getShapes();
    assertEquals(shapes1.size(), shapes2.size());

    for (int i = 0; i < shapes1.size(); i++) {
      XSSFShape sh1 = shapes1.get(i);
      XSSFShape sh2 = shapes2.get(i);

      assertTrue(sh1.getClass() == sh2.getClass());
      assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
    }

    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  }