[英]Adding a comment to a cell in an Excel file with Java using jxl library
I am trying to add a comment to a cell in Excel. I am using jxl library to do that:我正在尝试向 Excel 中的单元格添加评论。我正在使用 jxl 库来执行此操作:
cell = sheet.getWritableCell(1, 2); // cols, rows
WritableCellFeatures wcf = cell.getWritableCellFeatures();
wcf.setComment("comment2");
The last line returns: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
.最后一行返回:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
。 Despite many attempts I can't fix it.尽管进行了多次尝试,但我无法修复它。 Help will be appreciated.
帮助将不胜感激。 Thank you.
谢谢你。
--EDIT-- - 编辑 -
This is the addNumber method after modifications:这是修改后的 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
}
Have you previously added a cell at that location?您之前是否在该位置添加了单元格? The problem is that you can't set cell features on an
EmptyCell
and it will always return null
as its cell features.问题是您不能在
EmptyCell
上设置单元格特征,它总是会返回null
作为其单元格特征。
If you add a cell first, it works (try/catch omitted for clarity), as shown by the code below.如果您首先添加一个单元格,它将起作用(为清楚起见省略了 try/catch),如下面的代码所示。 Note that it also sets a
WritableCellFeatures
on the new Label
cell first, since initially, cell features are always null
.请注意,它还首先在新的
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();
Using this with the method in the OP:将其与 OP 中的方法一起使用:
I modified the method to return the created (and added!) Number
instance.我修改了返回创建(和添加!)
Number
实例的方法。 If you don't want that, you could instead retrieve the same cell using WritableWorkbook.getWritableCell()
using the same row/col.如果您不想这样,您可以使用
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;
}
From comments and:来自评论和:
I made this:我做的:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.