diff options
author | Dominik Stadler <centic@apache.org> | 2018-10-26 09:32:57 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2018-10-26 09:32:57 +0000 |
commit | a8fd70ec7fba1b66474f1fdde159b4764ad83c41 (patch) | |
tree | e0dd9cf5e13ad0c23ad843de6c0811daea3b2d90 /src/examples | |
parent | 1d464ed131575ae9b1616877cb59a580b8acecbd (diff) | |
download | poi-a8fd70ec7fba1b66474f1fdde159b4764ad83c41.tar.gz poi-a8fd70ec7fba1b66474f1fdde159b4764ad83c41.zip |
Adjust sample for creating comments to also create a .xlsx file
Enhance workbook factory to allow to create new empty workbooks as well
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1844881 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r-- | src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java index b14cc53abe..877708bde8 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java @@ -17,18 +17,22 @@ package org.apache.poi.hssf.usermodel.examples; -import java.io.FileOutputStream; -import java.io.IOException; - -import org.apache.poi.hssf.usermodel.HSSFCell; -import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFComment; -import org.apache.poi.hssf.usermodel.HSSFFont; -import org.apache.poi.hssf.usermodel.HSSFPatriarch; -import org.apache.poi.hssf.usermodel.HSSFRichTextString; -import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.ClientAnchor; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; + +import java.io.FileOutputStream; +import java.io.IOException; /** * Demonstrates how to work with excel cell comments.<p> @@ -39,46 +43,64 @@ import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; public class CellComments { public static void main(String[] args) throws IOException { - try (HSSFWorkbook wb = new HSSFWorkbook()) { - HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF"); + createWorkbook(false, ".xls"); + createWorkbook(true, ".xlsx"); + } + + private static void createWorkbook(boolean xssf, String extension) throws IOException { + try (Workbook wb = WorkbookFactory.create(xssf)) { + Sheet sheet = wb.createSheet("Cell comments in POI " + extension); + CreationHelper creationHelper = wb.getCreationHelper(); // Create the drawing patriarch. This is the top level container for all shapes including cell comments. - HSSFPatriarch patr = sheet.createDrawingPatriarch(); + Drawing<?> patr = sheet.createDrawingPatriarch(); //create a cell in row 3 - HSSFCell cell1 = sheet.createRow(3).createCell(1); - cell1.setCellValue(new HSSFRichTextString("Hello, World")); + Cell cell1 = sheet.createRow(3).createCell(1); + cell1.setCellValue(creationHelper.createRichTextString("Hello, World")); //anchor defines size and position of the comment in worksheet - HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); + ClientAnchor clientAnchor = creationHelper.createClientAnchor(); + clientAnchor.setCol1(4); + clientAnchor.setRow1(2); + clientAnchor.setCol2(6); + clientAnchor.setRow2(5); + Comment comment1 = patr.createCellComment(clientAnchor); // set text in the comment - comment1.setString(new HSSFRichTextString("We can set comments in POI")); + comment1.setString(creationHelper.createRichTextString("We can set comments in POI")); //set comment author. //you can see it in the status bar when moving mouse over the commented cell comment1.setAuthor("Apache Software Foundation"); - // The first way to assign comment to a cell is via HSSFCell.setCellComment method + // The first way to assign comment to a cell is via Cell.setCellComment method cell1.setCellComment(comment1); //create another cell in row 6 - HSSFCell cell2 = sheet.createRow(6).createCell(1); + Cell cell2 = sheet.createRow(6).createCell(1); cell2.setCellValue(36.6); - HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 8, (short) 6, 11)); - //modify background color of the comment - comment2.setFillColor(204, 236, 255); + clientAnchor = creationHelper.createClientAnchor(); + clientAnchor.setCol1(4); + clientAnchor.setRow1(8); + clientAnchor.setCol2(6); + clientAnchor.setRow2(11); + Comment comment2 = patr.createCellComment(clientAnchor); + //modify background color of the comment, only available in HSSF currently + if (wb instanceof HSSFWorkbook) { + ((HSSFComment) comment2).setFillColor(204, 236, 255); + } - HSSFRichTextString string = new HSSFRichTextString("Normal body temperature"); + RichTextString string = creationHelper.createRichTextString("Normal body temperature"); //apply custom font to the text in the comment - HSSFFont font = wb.createFont(); + Font font = wb.createFont(); font.setFontName("Arial"); font.setFontHeightInPoints((short) 10); font.setBold(true); - font.setColor(HSSFColorPredefined.RED.getIndex()); + font.setColor(IndexedColors.RED.getIndex()); string.applyFont(font); comment2.setString(string); @@ -94,7 +116,7 @@ public class CellComments { comment2.setRow(6); comment2.setColumn(1); - try (FileOutputStream out = new FileOutputStream("poi_comment.xls")) { + try (FileOutputStream out = new FileOutputStream("poi_comment" + extension)) { wb.write(out); } } |