Search in sources :

Example 6 with Comment

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

the class ExcelUtil method addComment.

/**
	 * 특정셀에 코멘트를 추가한다.
	 *
	 * @param sheet
	 * @param cell
	 * @param commentText
	 * @return
	 */
public static void addComment(Sheet sheet, Cell cell, String commentText) {
    XSSFDrawing patr = (XSSFDrawing) sheet.createDrawingPatriarch();
    Comment comment = patr.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
    comment.setString(new XSSFRichTextString(commentText));
    cell.setCellComment(comment);
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing)

Example 7 with Comment

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

the class CellComments method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    CreationHelper factory = wb.getCreationHelper();
    Sheet sheet = wb.createSheet();
    Cell cell1 = sheet.createRow(3).createCell(5);
    cell1.setCellValue("F4");
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = factory.createClientAnchor();
    Comment comment1 = drawing.createCellComment(anchor);
    RichTextString str1 = factory.createRichTextString("Hello, World!");
    comment1.setString(str1);
    comment1.setAuthor("Apache POI");
    cell1.setCellComment(comment1);
    Cell cell2 = sheet.createRow(2).createCell(2);
    cell2.setCellValue("C3");
    Comment comment2 = drawing.createCellComment(anchor);
    RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
    //apply custom font to the text in the comment
    Font font = wb.createFont();
    font.setFontName("Arial");
    font.setFontHeightInPoints((short) 14);
    font.setBold(true);
    font.setColor(IndexedColors.RED.getIndex());
    str2.applyFont(font);
    comment2.setString(str2);
    comment2.setAuthor("Apache POI");
    comment2.setAddress(new CellAddress("C3"));
    String fname = "comments.xlsx";
    FileOutputStream out = new FileOutputStream(fname);
    wb.write(out);
    out.close();
    wb.close();
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) RichTextString(org.apache.poi.ss.usermodel.RichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) CellAddress(org.apache.poi.ss.util.CellAddress) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 8 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 9 with Comment

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

the class TestCommentsTable method bug54920.

@Test
public void bug54920() throws IOException {
    final Workbook workbook = new XSSFWorkbook();
    final Sheet sheet = workbook.createSheet("sheet01");
    // create anchor
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();
    // place comment in A1
    // NOTE - only occurs if a comment is placed in A1 first
    Cell A1 = getCell(sheet, 0, 0);
    //Cell A1 = getCell(sheet, 2, 2);
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    setComment(sheet, A1, drawing, "for A1", helper, anchor);
    // find comment in A1 before we set the comment in B2
    Comment commentA1 = A1.getCellComment();
    assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
    assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
    // place comment in B2, according to Bug 54920 this removes the comment in A1!
    Cell B2 = getCell(sheet, 1, 1);
    setComment(sheet, B2, drawing, "for B2", helper, anchor);
    // find comment in A1
    Comment commentB2 = B2.getCellComment();
    assertEquals("should find correct comment in B2, but had null: " + commentB2, "for B2", commentB2.getString().getString());
    // find comment in A1
    commentA1 = A1.getCellComment();
    assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
    assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
    workbook.close();
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 10 with Comment

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

the class TestCommentsTable method existing.

@Test
public void existing() {
    Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
    Sheet sheet1 = workbook.getSheetAt(0);
    Sheet sheet2 = workbook.getSheetAt(1);
    assertTrue(((XSSFSheet) sheet1).hasComments());
    assertFalse(((XSSFSheet) sheet2).hasComments());
    // Comments should be in C5 and C7
    Row r5 = sheet1.getRow(4);
    Row r7 = sheet1.getRow(6);
    assertNotNull(r5.getCell(2).getCellComment());
    assertNotNull(r7.getCell(2).getCellComment());
    // Check they have what we expect
    // TODO: Rich text formatting
    Comment cc5 = r5.getCell(2).getCellComment();
    Comment cc7 = r7.getCell(2).getCellComment();
    assertEquals("Nick Burch", cc5.getAuthor());
    assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
    assertEquals(4, cc5.getRow());
    assertEquals(2, cc5.getColumn());
    assertEquals("Nick Burch", cc7.getAuthor());
    assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
    assertEquals(6, cc7.getRow());
    assertEquals(2, cc7.getColumn());
}
Also used : 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) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

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