2010-10-04 94 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

相關問題