Search in sources :

Example 6 with ClientAnchor

use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.

the class TestXSSFComment method testBug58175a.

@Ignore("Used for manual testing with opening the resulting Workbook in Excel")
@Test
public void testBug58175a() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString("Hello, World!");
        comment.setString(str);
        comment.setAuthor("Apache POI");
        /* fixed the problem as well 
             * comment.setColumn(cell.getColumnIndex());
             * comment.setRow(cell.getRowIndex());
             */
        // Assign the comment to the cell
        cell.setCellComment(comment);
        OutputStream out = new FileOutputStream("C:\\temp\\58175.xlsx");
        try {
            wb.write(out);
        } finally {
            out.close();
        }
    } finally {
        wb.close();
    }
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ClientAnchor

use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.

the class TestXSSFComment method testBug58175.

@Test
public void testBug58175() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
        // create comments and vmlDrawing parts if they don't exist
        CommentsTable comments = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getCommentsTable(true);
        XSSFVMLDrawing vml = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getVMLDrawing(true);
        CTShape vmlShape1 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
        }
        // create the comment in two different ways and verify that there is no difference
        XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
        shape1.setColumn(ca.getCol1());
        shape1.setRow(ca.getRow1());
        CTShape vmlShape2 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
        }
        CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
        XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
        assertEquals(shape1.getAuthor(), shape2.getAuthor());
        assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
        assertEquals(shape1.getColumn(), shape2.getColumn());
        assertEquals(shape1.getRow(), shape2.getRow());
        assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
        assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
        /*CommentsTable table1 = shape1.getCommentsTable();
            CommentsTable table2 = shape2.getCommentsTable();
            assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
            assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
            assertEquals(table1.getRelations(), table2.getRelations());*/
        assertEquals("The vmlShapes should have equal content afterwards", vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"));
    } finally {
        wb.close();
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) CTShape(com.microsoft.schemas.vml.CTShape) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) CommentsTable(org.apache.poi.xssf.model.CommentsTable) CellAddress(org.apache.poi.ss.util.CellAddress) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 8 with ClientAnchor

use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.

the class TestXSSFDrawing method testXSSFSAddPicture.

@Test
public void testXSSFSAddPicture() throws Exception {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet();
    //multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
    XSSFDrawing dr1 = sheet.createDrawingPatriarch();
    XSSFDrawing dr2 = sheet.createDrawingPatriarch();
    assertSame(dr1, dr2);
    List<RelationPart> rels = sheet.getRelationParts();
    assertEquals(1, rels.size());
    RelationPart rp = rels.get(0);
    assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
    assertEquals(0, rp.getDocumentPart().getRelations().size());
    XSSFDrawing drawing = rp.getDocumentPart();
    String drawingId = rp.getRelationship().getId();
    //there should be a relation to this drawing in the worksheet
    assertTrue(sheet.getCTWorksheet().isSetDrawing());
    assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
    byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");
    ClientAnchor anchor = wb1.getCreationHelper().createClientAnchor();
    anchor.setCol1(1);
    anchor.setRow1(1);
    drawing.createPicture(anchor, wb1.addPicture(pictureData, Workbook.PICTURE_TYPE_JPEG));
    final int pictureIndex = wb1.addPicture(pictureData, Workbook.PICTURE_TYPE_JPEG);
    drawing.createPicture(anchor, pictureIndex);
    drawing.createPicture(anchor, pictureIndex);
    // repeated additions of same share package relationship
    assertEquals(2, rp.getDocumentPart().getPackagePart().getRelationships().size());
    List<XSSFShape> shapes = drawing.getShapes();
    assertEquals(3, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFPicture);
    assertTrue(shapes.get(1) instanceof XSSFPicture);
    // Save and re-load it
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.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(3, ctDrawing.sizeOfTwoCellAnchorArray());
    shapes = dr1.getShapes();
    assertEquals(3, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFPicture);
    assertTrue(shapes.get(1) instanceof XSSFPicture);
    checkRewrite(wb2);
    wb2.close();
}
Also used : CTDrawing(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing) RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Test(org.junit.Test)

Example 9 with ClientAnchor

use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.

the class TestXSSFDrawing method testBug56835CellComment.

@Test(expected = IllegalArgumentException.class)
public void testBug56835CellComment() throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    XSSFDrawing drawing = sheet.createDrawingPatriarch();
    // first comment works
    ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
    XSSFComment comment = drawing.createCellComment(anchor);
    assertNotNull(comment);
    // Should fail if we try to add the same comment for the same cell
    try {
        drawing.createCellComment(anchor);
    } finally {
        wb.close();
    }
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Test(org.junit.Test)

Example 10 with ClientAnchor

use of org.apache.poi.ss.usermodel.ClientAnchor in project poi by apache.

the class TestXSSFChartTitle method createWorkbookWithChart.

private Workbook createWorkbookWithChart() {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("linechart");
    final int NUM_OF_ROWS = 3;
    final int NUM_OF_COLUMNS = 10;
    // Create a row and put some cells in it. Rows are 0 based.
    Row row;
    Cell cell;
    for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
        row = sheet.createRow((short) rowIndex);
        for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
            cell = row.createCell((short) colIndex);
            cell.setCellValue(colIndex * (rowIndex + 1));
        }
    }
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);
    LineChartData data = chart.getChartDataFactory().createLineChartData();
    // Use a category axis for the bottom axis.
    ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
    ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
    leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
    ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
    data.addSeries(xs, ys1);
    data.addSeries(xs, ys2);
    chart.plot(data, bottomAxis, leftAxis);
    return wb;
}
Also used : ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) Chart(org.apache.poi.ss.usermodel.Chart) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart)

Aggregations

ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)25 Sheet (org.apache.poi.ss.usermodel.Sheet)16 Workbook (org.apache.poi.ss.usermodel.Workbook)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)12 Test (org.junit.Test)12 Chart (org.apache.poi.ss.usermodel.Chart)11 Cell (org.apache.poi.ss.usermodel.Cell)9 ChartLegend (org.apache.poi.ss.usermodel.charts.ChartLegend)8 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)7 Row (org.apache.poi.ss.usermodel.Row)7 FileOutputStream (java.io.FileOutputStream)5 Comment (org.apache.poi.ss.usermodel.Comment)5 RichTextString (org.apache.poi.ss.usermodel.RichTextString)5 ChartAxis (org.apache.poi.ss.usermodel.charts.ChartAxis)5 ValueAxis (org.apache.poi.ss.usermodel.charts.ValueAxis)4 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)4 LineChartData (org.apache.poi.ss.usermodel.charts.LineChartData)3 ScatterChartData (org.apache.poi.ss.usermodel.charts.ScatterChartData)3 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)3 Dimension (java.awt.Dimension)2