gpt4 book ai didi

java - 如何使用 apache poi 为 3 个单元格设置注释

转载 作者:行者123 更新时间:2023-11-30 06:37:12 27 4
gpt4 key购买 nike

我想使用 Apache POI 为 3 个 excel 单元格设置评论。

这是我的源代码:

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表中没有显示?这里有什么不正确的地方?

最佳答案

有一次你问了一个问题,这让我尝试了一段时间的代码。

你的问题是这一行出现了 3 次,一次在每条评论之前。

patr = sheet.createDrawingPatriarch();

来自 the docs对于这种方法,

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

因此,每次您创建 DrawingPatriarch 时,您之前的评论都会被删除。

所以只做一次,在 comment1 之前。另外 2 次,删除或注释掉这一行。

关于java - 如何使用 apache poi 为 3 个单元格设置注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3855779/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com