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

那麼對於這種狀況,咱們的代碼應該如何寫呢? 借花獻佛,我就用Apache POI官方提供的例子,而後加上一些註解,給你們看一下。本例子的測試代碼是基於POI-3.12的。apache
執行完後,將會生成上圖所示的Excel工做表單(sheet)app
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import java.io.IOException;
- import java.io.FileOutputStream;
-
- public class CellComments {
- public static void main(String[] args) throws IOException {
-
- XSSFWorkbook wb = new XSSFWorkbook();
-
-
- CreationHelper factory = wb.getCreationHelper();
-
-
- XSSFSheet sheet = wb.createSheet();
-
-
- Drawing drawing = sheet.createDrawingPatriarch();
-
- ClientAnchor anchor = factory.createClientAnchor();
-
-
- Cell cell0 = sheet.createRow(1).createCell(0);
-
- cell0.setCellValue("Test");
-
- Comment comment0 = drawing.createCellComment(anchor);
- RichTextString str0 = factory.createRichTextString("Hello, World!");
- comment0.setString(str0);
- comment0.setAuthor("Apache POI");
- cell0.setCellComment(comment0);
-
-
- Cell cell1 = sheet.createRow(3).createCell(5);
-
- cell1.setCellValue("F4");
-
- Comment comment1 = drawing.createCellComment(anchor);
- RichTextString str1 = factory.createRichTextString("Hello, World!");
- comment1.setString(str1);
- comment1.setAuthor("Apache POI");
- cell1.setCellComment(comment1);
-
-
- Cell cell2 = sheet.createRow(2).createCell(2);
- cell2.setCellValue("C3");
-
- Comment comment2 = drawing.createCellComment(anchor);
- RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
-
- Font font = wb.createFont();
- font.setFontName("Arial");
- font.setFontHeightInPoints((short)14);
- font.setBoldweight(Font.BOLDWEIGHT_BOLD);
- font.setColor(IndexedColors.RED.getIndex());
- str2.applyFont(font);
-
- comment2.setString(str2);
- comment2.setAuthor("Apache POI");
- comment2.setColumn(2);
- comment2.setRow(2);
-
- String fname = "comments.xlsx";
- FileOutputStream out = new FileOutputStream(fname);
- wb.write(out);
- out.close();
-
- }
- }