Sometimes, we need to manipulate Apache POI to generate cell data while generating the cell, plus annotations (comments), similar to the following.
So how do we write our code in this case? Jiehuaxianfo, I'll use the official Apache POI example, and then add some annotations, to show you. The test code for this example is based on POI-3.12.
After execution, the Excel worksheet shown (sheet) will be generated
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;/** * Demonstrates how to work with Excel cell comments. * <p> * Excel comment is a kind of a text shape, * So inserting a comment are very similar to placing a text box in a Worksheet * </p> * * @author Yegor Kozlov */public class Cellcomments {public static void main (string[] args) t Hrows IOException {//1. Create a Workbook object Xssfworkbook wb = new Xssfworkbook (); 2. Get a POI tool class Creationhelper factory = Wb.getcreationhelper (); 3. Create a worksheet Xssfsheet sheet = Wb.createsheet (); 4. Get an object for Drawing Drawing = Sheet.createdrawingpatriarch (); 5. Clientanchor is an object attached to the worksheet, which is fixed in the upper-left and lower-right corners of a cell. Clientanchor anchor = Factory.createclientanchor (); 6. Create a cell (2 a cell) cell cell0 = Sheet.createrow (1). CreateCell (0); 6.1. Set the value for this cell cell0.setcellvalue ("Test"); 6.2. Add annotations to this cell Comment comment0 = drawing.createcellcomment (anchor); Richtextstring STR0 = factory.createrichtextstring ("Hello, world!"); Comment0.setstring (STR0); Comment0.setauthor ("Apache POI"); Cell0.setcellcomment (COMMENT0); 7. Create a cell (4F cell) Cell cell1 = Sheet.createrow (3). Createcell (5); 7.1. Set the value for this cell cell1.setcellvalue ("F4"); 7.2. Add annotations to this cell Comment comment1 = drawing.createcellcomment (anchor); Richtextstring str1 = factory.createrichtextstring ("Hello, world!"); Comment1.setstring (STR1); Comment1.setauthor ("Apache POI"); Cell1.setcellcomment (COMMENT1); 8. Create a cell (4F cell) Cell cell2 = Sheet.createrow (2). Createcell (2); Cell2.setcellvalue ("C3"); Comment Comment2 = drawing.createcellcomment (anchor); Richtextstring str2 = factory.createrichtextstring ("XSSF can set cell comments "); 9. Set fonts for annotations 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); 10. Save As Excel file String fname = "comments.xlsx"; FileOutputStream out = new FileOutputStream (fname); Wb.write (out); Out.close (); }}
(3) How to use Apache POI to manipulate Excel files-----How to annotate a cell?