1. 程式人生 > >java poi給sheet表格中的某個單元格新增批註

java poi給sheet表格中的某個單元格新增批註

// 建立工作簿
XSSFWorkbook workBook = new XSSFWorkbook(inputStream);
// 建立sheet
XSSFSheet sheet = workBook.createSheet(sheetName);
XSSFDrawing draw = newSheet.createDrawingPatriarch();
// 定義註釋的大小和位置
/*
 * @param dx1  the x coordinate within the first cell.
 * @param dy1  the y coordinate within the first cell.
 * @param
dx2 the x coordinate within the second cell. * @param dy2 the y coordinate within the second cell. * @param col1 the column (0 based) of the first cell. * @param row1 the row (0 based) of the first cell. * @param col2 the column (0 based) of the second cell. * @param row2 the row (0 based) of the second cell. */
XSSFComment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, 4, 2, 9, 7)); XSSFRichTextString rtf = new XSSFRichTextString("新增批註內容!"); XSSFFont commentFormatter = workBook.createFont(); commentFormatter.setFontName("宋體"); //設定字型大小 commentFormatter.setFontHeightInPoints((short) 9); rtf.applyFont(commentFormatter); comment.setString(rtf); comment.setAuthor("Zhihui Li"
); // 將批註新增到單元格物件中 sheet.getRow(rowIndex).getCell(columnIndex).setCellComment(comment);

如果有不熟悉如何操作Excel的,可以參考前面的博文:
poi操作excel總結