Esempio n. 1
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));
  }
Esempio n. 2
0
  /** Test reading text from a textbox in an existing file */
  public void testReadTextBox() {
    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(4) instanceof XSSFSimpleShape);

    XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
    assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());

    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  }
Esempio n. 3
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));
  }
Esempio n. 4
0
  /** Test reading multiple paragraphs from a textbox in an existing file */
  public void testReadTextBoxParagraphs() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox.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(1, shapes.size());

    assertTrue(shapes.get(0) instanceof XSSFSimpleShape);

    XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(0);

    List<XSSFTextParagraph> paras = textbox.getTextParagraphs();
    assertEquals(3, paras.size());

    assertEquals("Line 2", paras.get(1).getText()); // check content of second paragraph

    assertEquals("Line 1\nLine 2\nLine 3", textbox.getText()); // check content of entire textbox

    // check attributes of paragraphs
    assertEquals(TextAlign.LEFT, paras.get(0).getTextAlign());
    assertEquals(TextAlign.CENTER, paras.get(1).getTextAlign());
    assertEquals(TextAlign.RIGHT, paras.get(2).getTextAlign());

    Color clr = paras.get(0).getTextRuns().get(0).getFontColor();
    assertArrayEquals(
        new int[] {255, 0, 0}, new int[] {clr.getRed(), clr.getGreen(), clr.getBlue()});

    clr = paras.get(1).getTextRuns().get(0).getFontColor();
    assertArrayEquals(
        new int[] {0, 255, 0}, new int[] {clr.getRed(), clr.getGreen(), clr.getBlue()});

    clr = paras.get(2).getTextRuns().get(0).getFontColor();
    assertArrayEquals(
        new int[] {0, 0, 255}, new int[] {clr.getRed(), clr.getGreen(), clr.getBlue()});

    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  }
Esempio n. 5
0
  public void testNew() throws Exception {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    // multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
    XSSFDrawing dr1 = sheet.createDrawingPatriarch();
    XSSFDrawing dr2 = sheet.createDrawingPatriarch();
    assertSame(dr1, dr2);

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

    XSSFDrawing drawing = (XSSFDrawing) rels.get(0);
    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());

    XSSFConnector c1 = drawing.createConnector(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 2, 2));
    c1.setLineWidth(2.5);
    c1.setLineStyle(1);

    XSSFShapeGroup c2 = drawing.createGroup(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 5, 5));
    assertNotNull(c2);

    XSSFSimpleShape c3 = drawing.createSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
    c3.setText(new XSSFRichTextString("Test String"));
    c3.setFillColor(128, 128, 128);

    XSSFTextBox c4 = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 4, 4, 5, 6));
    XSSFRichTextString rt = new XSSFRichTextString("Test String");
    rt.applyFont(0, 5, wb.createFont());
    rt.applyFont(5, 6, wb.createFont());
    c4.setText(rt);

    c4.setNoFill(true);
    assertEquals(4, drawing.getCTDrawing().sizeOfTwoCellAnchorArray());

    List<XSSFShape> shapes = drawing.getShapes();
    assertEquals(4, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFConnector);
    assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
    assertTrue(shapes.get(2) instanceof XSSFSimpleShape);
    assertTrue(shapes.get(3) instanceof XSSFSimpleShape); //

    // Save and re-load it
    wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.getSheetAt(0);

    // Check
    dr1 = sheet.createDrawingPatriarch();
    CTDrawing ctDrawing = dr1.getCTDrawing();

    // Connector, shapes and text boxes are all two cell anchors
    assertEquals(0, ctDrawing.sizeOfAbsoluteAnchorArray());
    assertEquals(0, ctDrawing.sizeOfOneCellAnchorArray());
    assertEquals(4, ctDrawing.sizeOfTwoCellAnchorArray());

    shapes = dr1.getShapes();
    assertEquals(4, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFConnector);
    assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
    assertTrue(shapes.get(2) instanceof XSSFSimpleShape);
    assertTrue(shapes.get(3) instanceof XSSFSimpleShape); //

    // Ensure it got the right namespaces
    String xml = ctDrawing.toString();
    assertTrue(
        xml.contains(
            "xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
    assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));

    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  }