如何用Apache POI操做Excel文件-----如何對一個單元格加註解?

有的時候,咱們須要經過操做Apache POI,在生成Cell數據的同時,能對其生成的Cell,加上註解(comments),相似於下面的。java

那麼對於這種狀況,咱們的代碼應該如何寫呢? 借花獻佛,我就用Apache POI官方提供的例子,而後加上一些註解,給你們看一下。本例子的測試代碼是基於POI-3.12的。apache

執行完後,將會生成上圖所示的Excel工做表單(sheet)app

 

[java]  view plain  copy
 
  1. import org.apache.poi.ss.usermodel.*;  
  2. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  4. import java.io.IOException;  
  5. import java.io.FileOutputStream;  
  6.   
  7. /** 
  8.  * Demonstrates how to work with excel cell comments. 
  9.  * <p> 
  10.  * Excel comment is a kind of a text shape, 
  11.  * so inserting a comment is very similar to placing a text box in a worksheet 
  12.  * </p> 
  13.  * 
  14.  * @author Yegor Kozlov 
  15.  */  
  16. public class CellComments {  
  17.     public static void main(String[] args) throws IOException {  
  18.         //1.建立一個工做簿對象  
  19.         XSSFWorkbook wb = new XSSFWorkbook();  
  20.   
  21.         //2.獲得一個POI的工具類  
  22.         CreationHelper factory = wb.getCreationHelper();  
  23.   
  24.         //3. 建立一個工做表  
  25.         XSSFSheet sheet = wb.createSheet();  
  26.           
  27.         //4.獲得一個換圖的對象  
  28.         Drawing drawing = sheet.createDrawingPatriarch();  
  29.         //5. ClientAnchor是附屬在WorkSheet上的一個對象,  其固定在一個單元格的左上角和右下角.  
  30.         ClientAnchor anchor = factory.createClientAnchor();  
  31.           
  32.         //6. 建立一個單元格(2A單元格)  
  33.         Cell cell0 = sheet.createRow(1).createCell(0);  
  34.         //6.1. 對這個單元格設置值  
  35.         cell0.setCellValue("Test");  
  36.         //6.2. 對這個單元格加上註解  
  37.         Comment comment0 = drawing.createCellComment(anchor);  
  38.         RichTextString str0 = factory.createRichTextString("Hello, World!");  
  39.         comment0.setString(str0);  
  40.         comment0.setAuthor("Apache POI");  
  41.         cell0.setCellComment(comment0);  
  42.           
  43.         //7. 建立一個單元格(4F單元格)  
  44.         Cell cell1 = sheet.createRow(3).createCell(5);  
  45.         //7.1. 對這個單元格設置值  
  46.         cell1.setCellValue("F4");  
  47.         //7.2. 對這個單元格加上註解  
  48.         Comment comment1 = drawing.createCellComment(anchor);  
  49.         RichTextString str1 = factory.createRichTextString("Hello, World!");  
  50.         comment1.setString(str1);  
  51.         comment1.setAuthor("Apache POI");  
  52.         cell1.setCellComment(comment1);  
  53.   
  54.         //8. 建立一個單元格(4F單元格)  
  55.         Cell cell2 = sheet.createRow(2).createCell(2);  
  56.         cell2.setCellValue("C3");  
  57.   
  58.         Comment comment2 = drawing.createCellComment(anchor);  
  59.         RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");  
  60.         //9。爲註解設置字體  
  61.         Font font = wb.createFont();  
  62.         font.setFontName("Arial");  
  63.         font.setFontHeightInPoints((short)14);  
  64.         font.setBoldweight(Font.BOLDWEIGHT_BOLD);  
  65.         font.setColor(IndexedColors.RED.getIndex());  
  66.         str2.applyFont(font);  
  67.   
  68.         comment2.setString(str2);  
  69.         comment2.setAuthor("Apache POI");  
  70.         comment2.setColumn(2);  
  71.         comment2.setRow(2);  
  72.         //10. 保存成Excel文件  
  73.         String fname = "comments.xlsx";  
  74.         FileOutputStream out = new FileOutputStream(fname);  
  75.         wb.write(out);  
  76.         out.close();  
  77.   
  78.     }  
  79. }  
相關文章
相關標籤/搜索