使用jxl库在Java中向Excel文件的单元格添加注释

5

我正在尝试向Excel单元格添加注释。 我使用jxl库来完成这项工作:

   cell = sheet.getWritableCell(1, 2); // cols, rows
   WritableCellFeatures wcf = cell.getWritableCellFeatures();
   wcf.setComment("comment2");

最后一行返回:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException。尽管我尝试了很多次,但无法修复它。希望能得到帮助。谢谢。
--编辑-- 这是修改后的addNumber方法:
private static void addNumber(WritableSheet sheet, int column, int row,
        double d) throws WriteException, RowsExceededException {

    Number number;
    number = new Number(column, row, d, timesStandard);

    //sheet.addCell(number); // need to add the cell first

    if (user wants to add a comment) {
        WritableCellFeatures wcf = new WritableCellFeatures();
        wcf.setComment("the comment");
        number.setCellFeatures(wcf);
    }

    //sheet.addCell(number); // but may need to add the cell with comments as well
}

1
cell.getWritableCellFeatures()返回空值,因为该单元格没有任何特性。尝试使用WritableCellFeatures wcf = new WritableCellFeatures(); wcf.setComment("comment"); cell.setCellFeatures(wcf); - alain.janinm
1
在你的if语句中,你能加上Cell cell = sheet.getWritableCell(column, row);,然后用cell.setCellFeatures(wcf);替换number.setCellFeatures(wcf);吗? - alain.janinm
我添加了 WritableCell cell = sheet.getWritableCell(column, row); 并将 number 替换为 cell,但是注释不见了。 - Hurdler
那么你将 sheet.addCell(number); 替换为 sheet.addCell(cell); - alain.janinm
2个回答

3
您之前是否在该位置添加过单元格?问题在于您无法在空单元格上设置单元格特性,它总是会返回null作为其单元格特性。
如果您首先添加一个单元格,则可以解决此问题(为了清晰起见,省略了try/catch),如下所示的代码。请注意,它还首先在新的Label单元格上设置了WritableCellFeatures,因为最初,单元格特性始终为null
            WritableWorkbook book = Workbook.createWorkbook(new File("output.xls"));
            WritableSheet sheet = book.createSheet("Some Sheet", 0);

            Label label = new Label(1, 2, "Some label"); 
            sheet.addCell(label); // add cell!

            WritableCellFeatures wcf = new WritableCellFeatures();
            wcf.setComment("Hello!");

            // set cell features!
            label.setCellFeatures(wcf);

            book.write();
            book.close();

使用此方法与 OP 中的方法:

我修改了该方法,以返回创建的(并添加的!)Number 实例。如果您不想这样做,您可以使用相同行/列使用 WritableWorkbook.getWritableCell() 检索相同单元格。

public static void main(String[] args) throws IOException, RowsExceededException, WriteException {

    File file = new File("output.xls");
    WritableWorkbook workbook = Workbook.createWorkbook(file);
    WritableSheet sheet = workbook.createSheet("First Sheet", 0);

    Number number = addNumber(sheet, 3, 2, 565d);

    WritableCellFeatures wcf = number.getWritableCellFeatures();
    if (wcf == null) wcf = new WritableCellFeatures();
    wcf.setComment("the comment");
    number.setCellFeatures(wcf);

    workbook.write(); 
    workbook.close();

}

private static Number addNumber(WritableSheet sheet, int column, int row,
        double d) throws WriteException, RowsExceededException {

    Number number = new Number(column, row, d, timesStandard);
    sheet.addCell(number); // need to add the cell first
    return number;
}

谢谢您的回答,我刚刚检查了一下,评论已经添加了。但是,该单元格是使用以下方法创建的:private static void addNumber(WritableSheet sheet, int column, int row, double d) throws WriteException, RowsExceededException {,这似乎会覆盖注释。而且,评论是可选的,而此方法用于添加每个数字。 - Hurdler
addNumber 可能会设置新的单元格特性。在通过 addNumber 添加后,您需要获取可写单元格并执行操作。 - Torious
这段代码创建了一个带有文本“Some label”的单元格,并附带一条注释“Hello”。我需要使用addNumber方法来创建一个带有数字的单元格,然后可以选择添加注释。当我在创建您的“虚拟”单元格并尝试用我的数字替换内容后运行该方法时,注释会消失。 - Hurdler
在调用 addNumber() 后,您能否使用相同的坐标检索 WritableCell 并使用 getWritableCellFeatures() 检索其单元格特征,或者会导致返回 null?如果它是非 null 的,只需在获取的 WritableCellFeatures 上调用 setComment(),否则按上述设置新的 WritableCellFeatures - Torious
看起来是这样的;) 我明天会仔细检查它 - Hurdler
我只是将 if 条件从 (wcf == null) 改为了 boolean: (用户想要添加评论),所以我不需要 getWritableCellFeatures();,因为我总是在 if 中创建新的 wcf。感谢您的回答 ;) - Hurdler

2

从评论和以下文档:

我做了这个:

private static void addNumber(WritableSheet sheet, int column, int row,
    double d) throws WriteException, RowsExceededException {

   Number number;
   number = new Number(column, row, d, timesStandard);

   sheet.addCell(number);

   if (user wants to add a comment) {
     WritableCell cell = sheet.getWritableCell(column, row)
     Label l = (Label) cell;
     WritableCellFeatures cellFeatures = new WritableCellFeatures();
     cellFeatures.setComment("the cell comment");
     l.setCellFeatures(cellFeatures);
     sheet.addCell(l);
  }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接