2012-04-10 20 views
4

我正在尝试向Excel中的单元格添加注释。我使用JXL库来做到这一点:使用jxl库向使用Java的Excel文件中的单元格添加注释

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()'返回null,因为电池没有fea真实存在。尝试使用'WritableCellFeatures wcf = new WritableCellFeatures(); wcf.setComment( “意见”); cell.setCellFeatures(wcf);' – 2012-04-14 19:49:20

+1

在if语句中可以添加'Cell cell = sheet.getWritableCell(column,row);',然后用'cell.setCellFeatures(wcf)替换'number.setCellFeatures(wcf);' ;' – 2012-04-14 21:11:53

+0

我增加了'WritableCell cell = sheet.getWritableCell(column,row);'并用'cell'替换了'number',但注释不存在 – Hurdler 2012-04-14 21:20:00

回答

3

你有以前添加在那个位置的单元格?问题是您无法设置EmptyCell上的单元格功能,并且它始终会返回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; 
} 
+0

感谢您的回答,只是检查了它,并添加了评论。但是,该单元格是使用此方法创建的:'private static void addNumber(WritableSheet sheet,int column,int row,double d)throws WriteException,RowsExceededException {'显然会覆盖注释。此外,该评论是可选的,而此方法用于添加每个数字。 – Hurdler 2012-04-14 20:19:45

+0

究竟是什么问题? – Torious 2012-04-14 20:23:01

+0

'addNumber'可能会设置新的单元格功能。您必须在通过“addNumber”添加可写入单元格后执行您的操作。 – Torious 2012-04-14 20:28:15

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); 
    } 

} 
相关问题