001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hssf.usermodel.examples;
018:
019: import org.apache.poi.hssf.usermodel.*;
020: import org.apache.poi.hssf.util.HSSFColor;
021:
022: import java.io.*;
023:
024: /**
025: * Demonstrates how to work with excel cell comments.
026: *
027: * <p>
028: * Excel comment is a kind of a text shape,
029: * so inserting a comment is very similar to placing a text box in a worksheet
030: * </p>
031: *
032: * @author Yegor Kozlov
033: */
034: public class CellComments {
035:
036: public static void main(String[] args) throws IOException {
037:
038: HSSFWorkbook wb = new HSSFWorkbook();
039: HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
040:
041: // Create the drawing patriarch. This is the top level container for all shapes including cell comments.
042: HSSFPatriarch patr = sheet.createDrawingPatriarch();
043:
044: //create a cell in row 3
045: HSSFCell cell1 = sheet.createRow(3).createCell((short) 1);
046: cell1.setCellValue(new HSSFRichTextString("Hello, World"));
047:
048: //anchor defines size and position of the comment in worksheet
049: HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(
050: 0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
051:
052: // set text in the comment
053: comment1.setString(new HSSFRichTextString(
054: "We can set comments in POI"));
055:
056: //set comment author.
057: //you can see it in the status bar when moving mouse over the commented cell
058: comment1.setAuthor("Apache Software Foundation");
059:
060: // The first way to assign comment to a cell is via HSSFCell.setCellComment method
061: cell1.setCellComment(comment1);
062:
063: //create another cell in row 6
064: HSSFCell cell2 = sheet.createRow(6).createCell((short) 1);
065: cell2.setCellValue(36.6);
066:
067: HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(
068: 0, 0, 0, 0, (short) 4, 8, (short) 6, 11));
069: //modify background color of the comment
070: comment2.setFillColor(204, 236, 255);
071:
072: HSSFRichTextString string = new HSSFRichTextString(
073: "Normal body temperature");
074:
075: //apply custom font to the text in the comment
076: HSSFFont font = wb.createFont();
077: font.setFontName("Arial");
078: font.setFontHeightInPoints((short) 10);
079: font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
080: font.setColor(HSSFColor.RED.index);
081: string.applyFont(font);
082:
083: comment2.setString(string);
084: comment2.setVisible(true); //by default comments are hidden. This one is always visible.
085:
086: comment2.setAuthor("Bill Gates");
087:
088: /**
089: * The second way to assign comment to a cell is to implicitly specify its row and column.
090: * Note, it is possible to set row and column of a non-existing cell.
091: * It works, the commnet is visible.
092: */
093: comment2.setRow(6);
094: comment2.setColumn((short) 1);
095:
096: FileOutputStream out = new FileOutputStream("poi_comment.xls");
097: wb.write(out);
098: out.close();
099:
100: }
101: }
|