Search in sources :

Example 11 with Comment

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

the class TestXSSFSheet method addComments.

private void addComments(CreationHelper helper, Sheet sheet) {
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    for (int i = 0; i < 2; i++) {
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(0);
        anchor.setRow1(0 + i);
        anchor.setCol2(2);
        anchor.setRow2(3 + i);
        Comment comment = drawing.createCellComment(anchor);
        comment.setString(helper.createRichTextString("BugTesting"));
        Row row = sheet.getRow(0 + i);
        if (row == null) {
            row = sheet.createRow(0 + i);
        }
        Cell cell = row.getCell(0);
        if (cell == null) {
            cell = row.createCell(0);
        }
        cell.setCellComment(comment);
    }
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Row(org.apache.poi.ss.usermodel.Row) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell)

Example 12 with Comment

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

the class XSSFExcelExtractor method getText.

/**
     * Retrieves the text contents of the file
     */
public String getText() {
    DataFormatter formatter;
    if (locale == null) {
        formatter = new DataFormatter();
    } else {
        formatter = new DataFormatter(locale);
    }
    StringBuffer text = new StringBuffer();
    for (Sheet sh : workbook) {
        XSSFSheet sheet = (XSSFSheet) sh;
        if (includeSheetNames) {
            text.append(sheet.getSheetName()).append("\n");
        }
        // Header(s), if present
        if (includeHeadersFooters) {
            text.append(extractHeaderFooter(sheet.getFirstHeader()));
            text.append(extractHeaderFooter(sheet.getOddHeader()));
            text.append(extractHeaderFooter(sheet.getEvenHeader()));
        }
        // Rows and cells
        for (Object rawR : sheet) {
            Row row = (Row) rawR;
            for (Iterator<Cell> ri = row.cellIterator(); ri.hasNext(); ) {
                Cell cell = ri.next();
                // Is it a formula one?
                if (cell.getCellTypeEnum() == CellType.FORMULA) {
                    if (formulasNotResults) {
                        String contents = cell.getCellFormula();
                        checkMaxTextSize(text, contents);
                        text.append(contents);
                    } else {
                        if (cell.getCachedFormulaResultTypeEnum() == CellType.STRING) {
                            handleStringCell(text, cell);
                        } else {
                            handleNonStringCell(text, cell, formatter);
                        }
                    }
                } else if (cell.getCellTypeEnum() == CellType.STRING) {
                    handleStringCell(text, cell);
                } else {
                    handleNonStringCell(text, cell, formatter);
                }
                // Output the comment, if requested and exists
                Comment comment = cell.getCellComment();
                if (includeCellComments && comment != null) {
                    // Replace any newlines with spaces, otherwise it
                    //  breaks the output
                    String commentText = comment.getString().getString().replace('\n', ' ');
                    checkMaxTextSize(text, commentText);
                    text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
                }
                if (ri.hasNext()) {
                    text.append("\t");
                }
            }
            text.append("\n");
        }
        // add textboxes
        if (includeTextBoxes) {
            XSSFDrawing drawing = sheet.getDrawingPatriarch();
            if (drawing != null) {
                for (XSSFShape shape : drawing.getShapes()) {
                    if (shape instanceof XSSFSimpleShape) {
                        String boxText = ((XSSFSimpleShape) shape).getText();
                        if (boxText.length() > 0) {
                            text.append(boxText);
                            text.append('\n');
                        }
                    }
                }
            }
        }
        // Finally footer(s), if present
        if (includeHeadersFooters) {
            text.append(extractHeaderFooter(sheet.getFirstFooter()));
            text.append(extractHeaderFooter(sheet.getOddFooter()));
            text.append(extractHeaderFooter(sheet.getEvenFooter()));
        }
    }
    return text.toString();
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) XSSFSimpleShape(org.apache.poi.xssf.usermodel.XSSFSimpleShape) XSSFShape(org.apache.poi.xssf.usermodel.XSSFShape) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter)

Example 13 with Comment

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

the class TestHSSFComment method testBug56380InsertTooManyComments.

@Test
public void testBug56380InsertTooManyComments() throws Exception {
    HSSFWorkbook workbook = new HSSFWorkbook();
    try {
        Sheet sheet = workbook.createSheet();
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        String comment = "c";
        for (int rowNum = 0; rowNum < 258; rowNum++) {
            sheet.createRow(rowNum);
        }
        // should still work, for some reason DrawingManager2.allocateShapeId() skips the first 1024...
        for (int count = 1025; count < 65535; count++) {
            int rowNum = count / 255;
            int cellNum = count % 255;
            Cell cell = sheet.getRow(rowNum).createCell(cellNum);
            try {
                Comment commentObj = insertComment(drawing, cell, comment + cellNum);
                assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("While adding shape number " + count, e);
            }
        }
        // this should now fail to insert
        Row row = sheet.createRow(257);
        Cell cell = row.createCell(0);
        insertComment(drawing, cell, comment + 0);
    } finally {
        workbook.close();
    }
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) RichTextString(org.apache.poi.ss.usermodel.RichTextString) 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 14 with Comment

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

the class TestHSSFComment method insertComment.

private Comment insertComment(Drawing<?> drawing, Cell cell, String message) {
    CreationHelper factory = cell.getSheet().getWorkbook().getCreationHelper();
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(cell.getRowIndex());
    anchor.setRow2(cell.getRowIndex() + 1);
    anchor.setDx1(100);
    anchor.setDx2(100);
    anchor.setDy1(100);
    anchor.setDy2(100);
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(message);
    comment.setString(str);
    comment.setAuthor("fanfy");
    cell.setCellComment(comment);
    return comment;
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Aggregations

Comment (org.apache.poi.ss.usermodel.Comment)14 Sheet (org.apache.poi.ss.usermodel.Sheet)8 Test (org.junit.Test)8 Cell (org.apache.poi.ss.usermodel.Cell)7 Row (org.apache.poi.ss.usermodel.Row)7 Workbook (org.apache.poi.ss.usermodel.Workbook)6 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)6 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)5 BaseTestCellComment (org.apache.poi.ss.usermodel.BaseTestCellComment)4 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)4 RichTextString (org.apache.poi.ss.usermodel.RichTextString)4 CellAddress (org.apache.poi.ss.util.CellAddress)4 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)4 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)4 XSSFClientAnchor (org.apache.poi.xssf.usermodel.XSSFClientAnchor)3 FileOutputStream (java.io.FileOutputStream)2 POITestCase.skipTest (org.apache.poi.POITestCase.skipTest)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2