簡體   English   中英

如何使用Apache POI為3個單元格設置注釋

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

我想使用Apache POI為3個excel單元設置注釋。

這是我的源代碼:

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();

    }
}

在運行程序時,注釋僅針對最后一個單元格設置。 但是我將前兩個單元的注釋打印得很好。 但未在Excel工作表中顯示嗎? 這里有什么不對的地方?

有一次,您提出了一個問題,讓我嘗試了一段時間的代碼。

您的問題是此行出現3次,每次評論之前。

patr = sheet.createDrawingPatriarch();

從該方法的文檔中,

創建頂級繪圖族長。 這將具有刪除此工作表上所有現有工程圖的效果。

因此,發生的情況是,每次創建DrawingPatriarch時,您先前的注釋都會被刪除。

因此,僅在comment1之前comment1一次。 其他2次,刪除或注釋掉這一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM