使用jxl库在使用Java的Excel文件中的单元格中添加注释 [英] Adding a comment to a cell in an Excel file with Java using jxl library

查看:197
本文介绍了使用jxl库在使用Java的Excel文件中的单元格中添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add a comment to a cell in Excel. I am using jxl library to do that:

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

最后一行返回:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.尽管进行了很多尝试,但我还是无法解决.帮助将不胜感激.谢谢你.

The last line returns: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. Despite many attempts I can't fix it. Help will be appreciated. Thank you.

-编辑-
这是修改后的addNumber方法:

--EDIT--
This is the addNumber method after modifications:

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
}

推荐答案

您以前是否在该位置添加了单元格?问题在于您不能在EmptyCell上设置单元格特征,并且它将始终返回null作为其单元格特征.

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.

如果首先添加一个单元格,它将起作用(为清晰起见,省略了try/catch),如下面的代码所示.请注意,它还首先在新的Label单元格上设置了WritableCellFeatures,因为最初,单元格特征始终为null.

If you add a cell first, it works (try/catch omitted for clarity), as shown by the code below. Note that it also sets a WritableCellFeatures on the new Label cell first, since initially, cell features are always 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()使用相同的行/列来检索相同的单元格.

I modified the method to return the created (and added!) Number instance. If you don't want that, you could instead retrieve the same cell using WritableWorkbook.getWritableCell() using the same row/col.

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

这篇关于使用jxl库在使用Java的Excel文件中的单元格中添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆