I am using the 3.0.1-FINAL version of POI. I was not successful in adding comments to more than one cell in a loop. I couldn't figure out what is happening to the comment objects I am creating using the command HSSFPatriarch.createComment() function. Please look at the code I am using bellow: Scenario1: Created a comment object, added it to the cell in a for loop --> expected result, all the cells in the excel sheet should have the same comment. ==> Result I am getting: Only the top most cell of the excel sheet is having the comment. The cell is choosen arbritrarliy at times. Scenario2: Created a comment object, added it to the cell. Created another comment object (but not added to any cell) in a for loop --> expected result: all the cells in the excel sheet should have the first comment ==> Result I am getting: One cell has the first comment and another has the second comment (please note, I've never added the second comment to any of the cells). The selection of the cells is arbitrary at times. My actual requirement is to add comments to selected cells. Your help in this regard is highly appreciated. Code I've used for adding comments : HSSFPatriarch patr = finalSheet.createDrawingPatriarch(); HSSFCell cell1 = finalSheet.createRow(4).createCell((short)1); HSSFCell cell = finalSheet.createRow(5).createCell((short)1); HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 100, 0, 0, (short)4, 2, (short) 6, 5)); comment1.setString(new HSSFRichTextString("help me")); comment1.setAuthor("SUMIT"); newCell.setCellComment(comment1); cell.setCellComment(comment1); cell1.setCellComment(comment1);
I have a feeling that a comment can only be added to one cell If you create+attach one comment per cell, does it all work?
--- Comment #1 from Nick Burch <nick@torchbox.com> 2008-03-27 05:02:44 PST --- I have a feeling that a comment can only be added to one cell If you create+attach one comment per cell, does it all work? -------------------------------------------Sumit i have tried this too. i am creating the object of HSSFComment and HSSFCell in a loop and try to set that new object of comment in the cell and that too in a loop expected result-->>>comment in all the cells result-->>only one comment comes and that too in arbitary cell
I tried creating object of HSSFCell and HSSFComment object in a loop andtry to set that new object of comment in new cell..... i.e. i m always creating a new object of comment for each cell but thatdoest not even work.
If you add comments with poi, save and re-load into poi, do they appear on the correct cells then? If not, could you please knock up a quick unit test for the problem? Just something that creates a new workbook and sheet, and adds a few cells, attaches comments to those. Write a to ByteArrayOutputStream, read in again from a ByteArrayInputStream, and assert that the comments are on the right cells (this bit will fail) We'll then have something to test future fixes against
Created attachment 21727 [details] unit test containing object of HSSFComment being created in a loop This test file creates object of cell and comment in a loop and the output is---> comment in only one cell this happens even after writing in ByteArrayOutputStream and then reading from ByteArrayInputStream
Created attachment 21728 [details] unit test containing one object of comment for all the cells This test file has only one comment for three cells and the output is -->>comment in only one cell this happens even after writing in ByteArrayOutputStream and then reading from ByteArrayInputStream
Created attachment 21729 [details] unit test containing one unused object of HSSFComment IN this test file i have created 3 cells and 2 comment objects and have not used one comment object The comment object which is not used is being displayed in excel file in the first cell which is total disaster...
Created attachment 21730 [details] unit test showing more than 1 comment in a excel file this test file will only generate the expected result while this is exactly same as in attachment no 21727
Hi, (1) A comment can be assigned to only one cell. If you assign the same comment object to multiple cells then only the last cell will have it. (2) HSSFPatriarch is a sheet-scope object, not comment-scope. Move it out of the loop and all will be fine: HSSFPatriarch patr = finalsheet.createDrawingPatriarch(); for(int i=0;i<3;i++) { HSSFCell cell1=finalsheet.createRow(2).createCell((short) i); HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 100, 0, 0, (short)4, 2, (short) 6, 5)); } I updated the docs to state it. (3) In the forth example you don't set comment text for comment2 and comment3. Excel doesn't like it and says the file is corrupted. Check the code: HSSFPatriarch patr = finalsheet.createDrawingPatriarch(); HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 100, 0, 0, (short)4, 2, (short) 6, 5)); comment1.setString(new HSSFRichTextString("help me 1")); HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 100, 0, 0, (short)4, 2, (short) 6, 5)); //should it be comment2 comment1.setString(new HSSFRichTextString("help me 2")); HSSFComment comment3 = patr.createComment(new HSSFClientAnchor(0, 100, 0, 0, (short)4, 2, (short) 6, 5)); //should it be comment3 comment1.setString(new HSSFRichTextString("help me 3")); cell1.setCellComment(comment1); cell2.setCellComment(comment2); cell3.setCellComment(comment3); It looks like a copy/paste bug. (4) Your problem has nothing to do with serialization. It doesn't matter if you write directly to FileOutputStream or serialize in a byte buffer and then to a file. Regards, Yegor