我正在尝试向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
}
cell.getWritableCellFeatures()
返回空值,因为该单元格没有任何特性。尝试使用WritableCellFeatures wcf = new WritableCellFeatures(); wcf.setComment("comment"); cell.setCellFeatures(wcf);
。 - alain.janinmCell cell = sheet.getWritableCell(column, row);
,然后用cell.setCellFeatures(wcf);
替换number.setCellFeatures(wcf);
吗? - alain.janinmWritableCell cell = sheet.getWritableCell(column, row);
并将number
替换为cell
,但是注释不见了。 - Hurdlersheet.addCell(number);
替换为sheet.addCell(cell);
? - alain.janinm