简体   繁体   English

如何使用Apache POI为3个单元格设置注释

[英]How to set comments for 3 cells using apache poi

I want to set comments for 3 excel cells using Apache POI. 我想使用Apache POI为3个excel单元设置注释。

This is my source code: 这是我的源代码:

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

public class CellComments
{
    public static void main(String[] args) throws IOException  {

      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");


      HSSFPatriarch patr = sheet.createDrawingPatriarch();
      HSSFCell cell1 = sheet.createRow(3).createCell((short)1);
      cell1.setCellValue(new HSSFRichTextString("Hello, World"));

      //anchor defines size and position of the comment in worksheet
      HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      comment1.setString(new HSSFRichTextString("FirstComments"));
      cell1.setCellComment(comment1);
      System.out.println("Cell comments: "+cell1.getCellComment().getString());

      patr = sheet.createDrawingPatriarch();
      comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      //HSSFComment comment2=patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      HSSFCell cell2 = sheet.createRow(6).createCell((short)1);
      cell2.setCellValue(36.6);
      comment1.setString(new HSSFRichTextString("second commetns"));
      cell2.setCellComment(comment1);
      System.out.println("Cell comments: "+cell2.getCellComment().getString());

      patr = sheet.createDrawingPatriarch();
      comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      //HSSFComment comment3=patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      cell2 = sheet.createRow(10).createCell((short)1);
      cell2.setCellValue(150);
      comment1.setString(new HSSFRichTextString("Third commetns"));
      cell2.setCellComment(comment1);
      System.out.println("Cell comments: "+cell2.getCellComment().getString());

      FileOutputStream out = new FileOutputStream("C:/Documents and Settings/saravanan/Desktop/cellcomments.xls");
      wb.write(out);
      out.close();

    }
}

While run the program, comments set only for last cell. 在运行程序时,注释仅针对最后一个单元格设置。 But I printed the comments for the first two cells it was printed fine. 但是我将前两个单元的注释打印得很好。 But not shown in the excel sheet? 但未在Excel工作表中显示吗? What's incorrect here? 这里有什么不对的地方?

For once you've asked a question which had me trying out the code for a while. 有一次,您提出了一个问题,让我尝试了一段时间的代码。

Your problem is this line appears 3 times, once before each comment. 您的问题是此行出现3次,每次评论之前。

patr = sheet.createDrawingPatriarch();

From the docs for this method, 从该方法的文档中,

Creates the top-level drawing patriarch. 创建顶级绘图族长。 This will have the effect of removing any existing drawings on this sheet. 这将具有删除此工作表上所有现有工程图的效果。

So whats happening is that your earlier comments are getting removed each time you create the DrawingPatriarch. 因此,发生的情况是,每次创建DrawingPatriarch时,您先前的注释都会被删除。

So do this only once, before comment1 . 因此,仅在comment1之前comment1一次。 The other 2 times, remove or comment out this line. 其他2次,删除或注释掉这一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM