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

时间:2012-04-10 21:46:26

标签: java excel jxl

我正在尝试在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
}

2 个答案:

答案 0 :(得分:3)

您之前是否在该位置添加了一个单元格?问题是您无法在EmptyCell上设置单元格功能,并且它将始终返回null作为其单元格功能。

如果您先添加一个单元格,它可以正常工作(为了清楚起见,省略了try / catch),如下面的代码所示。请注意,它还会首先在新的WritableCellFeatures单元格上设置Label,因为最初,单元格要素始终为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;
}

答案 1 :(得分: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);
  }

}