如何使用Apache POI设置为3细胞意见 [英] How to set comments for 3 cells using apache poi

查看:138
本文介绍了如何使用Apache POI设置为3细胞意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为使用Apache POI 3 Excel单元格的意见。

I want to set comments for 3 excel cells using Apache POI.

这是我的源$ C ​​$ C:

This is my source code:

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

public class CellComments
{
    public static void main(String[] args) throws IOException  {

      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");


      HSSFPatriarch patr = sheet.createDrawingPatriarch();
      HSSFCell cell1 = sheet.createRow(3).createCell((short)1);
      cell1.setCellValue(new HSSFRichTextString("Hello, World"));

      //anchor defines size and position of the comment in worksheet
      HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      comment1.setString(new HSSFRichTextString("FirstComments"));
      cell1.setCellComment(comment1);
      System.out.println("Cell comments: "+cell1.getCellComment().getString());

      patr = sheet.createDrawingPatriarch();
      comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      //HSSFComment comment2=patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      HSSFCell cell2 = sheet.createRow(6).createCell((short)1);
      cell2.setCellValue(36.6);
      comment1.setString(new HSSFRichTextString("second commetns"));
      cell2.setCellComment(comment1);
      System.out.println("Cell comments: "+cell2.getCellComment().getString());

      patr = sheet.createDrawingPatriarch();
      comment1 = patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      //HSSFComment comment3=patr.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 6, 5));
      cell2 = sheet.createRow(10).createCell((short)1);
      cell2.setCellValue(150);
      comment1.setString(new HSSFRichTextString("Third commetns"));
      cell2.setCellComment(comment1);
      System.out.println("Cell comments: "+cell2.getCellComment().getString());

      FileOutputStream out = new FileOutputStream("C:/Documents and Settings/saravanan/Desktop/cellcomments.xls");
      wb.write(out);
      out.close();

    }
}

在运行该程序,评论仅供网友对最后一个单元格设置。但是,我打印的前两个单元格这是印精细的意见。但在Excel工作表中没有显示?什么是不正确吗?

While run the program, comments set only for last cell. But I printed the comments for the first two cells it was printed fine. But not shown in the excel sheet? What's incorrect here?

推荐答案

有关,一旦你问其中有我尝试了code一段时间的问题。

For once you've asked a question which had me trying out the code for a while.

您的问题是这条线出现3次,每次评论之前一次。

Your problem is this line appears 3 times, once before each comment.

patr = sheet.createDrawingPatriarch();

从<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#createDrawingPatriarch%28%29\">the文档这种方法,

创建顶层图纸
  族长。这将具有的效果
  在删除任何现有的图纸
  此表。

Creates the top-level drawing patriarch. This will have the effect of removing any existing drawings on this sheet.

所以,什么情况是,你刚才的意见在每次创建DrawingPatriarch时间去删除。

So whats happening is that your earlier comments are getting removed each time you create the DrawingPatriarch.

所以做到这一点只有一次,之前注释1 。另外2次,删除或注释掉这一行。

So do this only once, before comment1. The other 2 times, remove or comment out this line.

这篇关于如何使用Apache POI设置为3细胞意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆