/** 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)); }
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)); }
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)); }
/** 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)); }
public void testXSSFSimpleShapeCausesNPE56514() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56514.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); XSSFDrawing drawing = sheet.createDrawingPatriarch(); List<XSSFShape> shapes = drawing.getShapes(); assertEquals(4, shapes.size()); wb = XSSFTestDataSamples.writeOutAndReadBack(wb); shapes = drawing.getShapes(); assertEquals(4, shapes.size()); /* OutputStream stream = new FileOutputStream(new File("C:\\temp\\56514.xlsx")); try { wb.write(stream); } finally { stream.close(); }*/ }
/** Test reading bullet numbering from a textbox in an existing file */ public void testReadTextBox2() { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox2.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); XSSFDrawing drawing = sheet.createDrawingPatriarch(); List<XSSFShape> shapes = drawing.getShapes(); XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(0); String extracted = textbox.getText(); StringBuilder sb = new StringBuilder(); sb.append("1. content1A\n"); sb.append("\t1. content1B\n"); sb.append("\t2. content2B\n"); sb.append("\t3. content3B\n"); sb.append("2. content2A\n"); sb.append("\t3. content2BStartAt3\n"); sb.append("\t\n\t\n\t"); sb.append("4. content2BStartAt3Incremented\n"); sb.append("\t\n\t\n\t\n\t"); assertEquals(sb.toString(), extracted); assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); }