Adding a comment to a cell in an Excel file with Java using jxl library

巧了我就是萌 提交于 2020-02-23 19:56:14

问题


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

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.

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

回答1:


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.

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

Using this with the method in the OP:

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



回答2:


From comments and :

  • http://www.docjar.org/html/api/jxl/demo/ReadWrite.java.html
  • http://kickjava.com/src/jxl/demo/Write.java.htm

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

}


来源:https://stackoverflow.com/questions/10096915/adding-a-comment-to-a-cell-in-an-excel-file-with-java-using-jxl-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!