Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Insert Cell Comment
The following example shows you how to insert a cell comment.
import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Run; import com.independentsoft.office.spreadsheet.Text; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.comments.Author; import com.independentsoft.office.spreadsheet.comments.Comment; import com.independentsoft.office.spreadsheet.comments.CommentSet; import com.independentsoft.office.vml.Anchor; import com.independentsoft.office.vml.ClientData; import com.independentsoft.office.vml.ConnectType; import com.independentsoft.office.vml.ExtensionHandlingBehavior; import com.independentsoft.office.vml.Fill; import com.independentsoft.office.vml.InsetMode; import com.independentsoft.office.vml.ObjectType; import com.independentsoft.office.vml.Position; import com.independentsoft.office.vml.Shadow; import com.independentsoft.office.vml.Shape; import com.independentsoft.office.vml.ShapeLayout; import com.independentsoft.office.vml.ShapePath; import com.independentsoft.office.vml.ShapeStyle; import com.independentsoft.office.vml.ShapeTemplate; import com.independentsoft.office.vml.Stroke; import com.independentsoft.office.vml.StrokeJoinStyle; import com.independentsoft.office.vml.TextBox; public class Example { public static void main(String[] args) { try { Worksheet sheet1 = new Worksheet(); sheet1.set("A1",new Cell(100)); Author john = new Author(); john.setName("John"); sheet1.setCommentSet(new CommentSet()); sheet1.getCommentSet().getAuthors().add(john); int johnID = sheet1.getCommentSet().getAuthors().size() - 1; Comment comment1 = new Comment(); comment1.setAuthorID(johnID); comment1.setCellReference("A1"); Run run1 = new Run(); run1.setText(new Text("Author: John\r\n")); run1.setBold(true); Run run2 = new Run(); run2.setText(new Text("This is a comment")); comment1.getRuns().add(run1); comment1.getRuns().add(run2); //Create comment text box ShapeLayout layout1 = new ShapeLayout(); layout1.setExtensionHandlingBehavior(ExtensionHandlingBehavior.EDITABLE); ShapeTemplate template1 = new ShapeTemplate(); template1.setID("template1"); template1.setCoordinateSpaceSize("21600,21600"); template1.setEdgePath("m,l,21600r21600,l21600,xe"); template1.setOptionalNumberID(202); Stroke stroke1 = new Stroke(); stroke1.setJoinStyle(StrokeJoinStyle.MITER); ShapePath path1 = new ShapePath(); path1.setEnableGradient(true); path1.setConnectionPointType(ConnectType.FOUR); template1.getContent().add(stroke1); template1.getContent().add(path1); Shape shape1 = new Shape(); shape1.setID("1"); shape1.setTypeReference("template1"); shape1.setFillColor("#FFFFE1"); shape1.setTextInsetMode(InsetMode.AUTO); ShapeStyle style1 = new ShapeStyle(); style1.setPosition(Position.ABSOLUTE); style1.setLeftMargin("59.25pt"); style1.setTopMargin("1.5pt"); style1.setWidth("96pt"); style1.setHeight("55.5pt"); style1.setZIndex("1"); Fill fill1 = new Fill(); fill1.setSecondaryColor("#FFFFE1"); Shadow shadow1 = new Shadow(); shadow1.setDisplay(true); shadow1.setPrimaryColor("black"); shadow1.setTransparent(true); TextBox textBox1 = new TextBox(); ClientData clientData1 = new ClientData(); clientData1.setObjectType(ObjectType.NOTE); clientData1.setMoveWithCells(true); clientData1.setSizeWithCells(true); clientData1.setAnchor(new Anchor(1,15,0,2,3,15,3,16)); clientData1.setCommentRow(0); clientData1.setCommentColumn(0); clientData1.setCommentVisibility(true); shape1.getContent().add(fill1); shape1.getContent().add(shadow1); shape1.getContent().add(textBox1); shape1.getContent().add(clientData1); sheet1.getCommentSet().getComments().add(comment1); sheet1.getVmlObjects().add(layout1); sheet1.getVmlObjects().add(template1); sheet1.getVmlObjects().add(shape1); Workbook book = new Workbook(); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*