Search in sources :

Example 41 with Row

use of org.apache.poi.ss.usermodel.Row 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 42 with Row

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

the class TestXSSFComment method bug57838DeleteRowsWthCommentsBug.

@Test
public void bug57838DeleteRowsWthCommentsBug() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57838.xlsx");
    Sheet sheet = wb.getSheetAt(0);
    Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
    assertNotNull(comment1);
    Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
    assertNotNull(comment2);
    Row row = sheet.getRow(2);
    assertNotNull(row);
    // Remove row from index 2
    sheet.removeRow(row);
    row = sheet.getRow(2);
    // Row is null since we deleted it.
    assertNull(row);
    comment1 = sheet.getCellComment(new CellAddress(2, 1));
    // comment should be null but will fail due to bug
    assertNull(comment1);
    comment2 = sheet.getCellComment(new CellAddress(2, 2));
    // comment should be null but will fail due to bug
    assertNull(comment2);
    wb.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 43 with Row

use of org.apache.poi.ss.usermodel.Row 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 44 with Row

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

the class TestXSSFDataValidation method createValidation.

private XSSFDataValidation createValidation(XSSFSheet sheet) {
    //create the cell that will have the validation applied
    final Row row = sheet.createRow(0);
    row.createCell(0);
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("true");
    final XSSFDataValidation validation = (XSSFDataValidation) dataValidationHelper.createValidation(constraint, new CellRangeAddressList(0, 0, 0, 0));
    return validation;
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) Row(org.apache.poi.ss.usermodel.Row) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper)

Example 45 with Row

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

the class TestXSSFHyperlink method testLoadSave.

@Test
public void testLoadSave() {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
    CreationHelper createHelper = workbook.getCreationHelper();
    assertEquals(3, workbook.getNumberOfSheets());
    XSSFSheet sheet = workbook.getSheetAt(0);
    // Check hyperlinks
    assertEquals(4, sheet.getNumHyperlinks());
    doTestHyperlinkContents(sheet);
    // Write out, and check
    // Load up again, check all links still there
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    assertEquals(3, wb2.getNumberOfSheets());
    assertNotNull(wb2.getSheetAt(0));
    assertNotNull(wb2.getSheetAt(1));
    assertNotNull(wb2.getSheetAt(2));
    sheet = wb2.getSheetAt(0);
    // Check hyperlinks again
    assertEquals(4, sheet.getNumHyperlinks());
    doTestHyperlinkContents(sheet);
    // Add one more, and re-check
    Row r17 = sheet.createRow(17);
    Cell r17c = r17.createCell(2);
    Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
    hyperlink.setAddress("http://poi.apache.org/spreadsheet/");
    hyperlink.setLabel("POI SS Link");
    r17c.setHyperlink(hyperlink);
    assertEquals(5, sheet.getNumHyperlinks());
    doTestHyperlinkContents(sheet);
    assertEquals(HyperlinkType.URL, sheet.getRow(17).getCell(2).getHyperlink().getTypeEnum());
    assertEquals("POI SS Link", sheet.getRow(17).getCell(2).getHyperlink().getLabel());
    assertEquals("http://poi.apache.org/spreadsheet/", sheet.getRow(17).getCell(2).getHyperlink().getAddress());
    // Save and re-load once more
    XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
    assertEquals(3, wb3.getNumberOfSheets());
    assertNotNull(wb3.getSheetAt(0));
    assertNotNull(wb3.getSheetAt(1));
    assertNotNull(wb3.getSheetAt(2));
    sheet = wb3.getSheetAt(0);
    assertEquals(5, sheet.getNumHyperlinks());
    doTestHyperlinkContents(sheet);
    assertEquals(HyperlinkType.URL, sheet.getRow(17).getCell(2).getHyperlink().getTypeEnum());
    assertEquals("POI SS Link", sheet.getRow(17).getCell(2).getHyperlink().getLabel());
    assertEquals("http://poi.apache.org/spreadsheet/", sheet.getRow(17).getCell(2).getHyperlink().getAddress());
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) HSSFHyperlink(org.apache.poi.hssf.usermodel.HSSFHyperlink) BaseTestHyperlink(org.apache.poi.ss.usermodel.BaseTestHyperlink) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink) Test(org.junit.Test)

Aggregations

Row (org.apache.poi.ss.usermodel.Row)316 Cell (org.apache.poi.ss.usermodel.Cell)230 Sheet (org.apache.poi.ss.usermodel.Sheet)179 Workbook (org.apache.poi.ss.usermodel.Workbook)125 Test (org.junit.Test)116 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)55 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)44 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)35 CellStyle (org.apache.poi.ss.usermodel.CellStyle)27 CellReference (org.apache.poi.ss.util.CellReference)22 ArrayList (java.util.ArrayList)21 FileOutputStream (java.io.FileOutputStream)20 IOException (java.io.IOException)17 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)17 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)17 HashMap (java.util.HashMap)16 RichTextString (org.apache.poi.ss.usermodel.RichTextString)16 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)16 List (java.util.List)14 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)14