2010-10-04 197 views
2

我想使用Apache POI为3个excel单元格设置注释。如何使用apache poi设置3个单元格的注释

这是我的源代码:

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表中显示?这里有什么不正确?

+0

伟大的问题!教我如何与NPOI写评论。谢谢。 – 2013-07-18 15:37:40

回答

5

有一次,你问了一个问题,让我尝试一段时间的代码。

你的问题是这条线出现3次,每次评论前一次。

patr = sheet.createDrawingPatriarch(); 

the docs从用于该方法中,

创建的顶层绘图 族长。这将影响 移除此工作表中所有现有图纸 。

因此,发生的事情是,您每次创建DrawingPatriarch时,先前的注释都会被删除。

所以这样做只有一次,在comment1之前。另外2次,删除或注释掉这一行。

+0

谢谢jose .... – Saravanan 2010-10-06 09:37:27

+0

@Saravanan:这里没有私人学费。询问一个适当的问题,你会得到社区的回复。看到这个网址如何提出好问题。 www.tinyurl.com/so-hints – JoseK 2010-10-07 05:05:17

相关问题