Java Apache POI:一个单元格中不允许有多个单元格注释

Java Apache POI:一个单元格中不允许有多个单元格注释,java,excel,spring-boot,apache-poi,xssf,Java,Excel,Spring Boot,Apache Poi,Xssf,我正在尝试向单元格添加注释。代码在两行中运行良好。但当行号变为第三行时,它开始给我错误信息:- java.lang.IllegalArgumentException:一个单元格中不允许有多个单元格注释 当我在读取单元格值时检测到异常时,我添加注释。下面给出了我用于添加注释的方法:- private void cellException(Sheet datatypeSheet, CellStyle cellErrorStyle, Row invCurrentRow, int inv

我正在尝试向单元格添加注释。代码在两行中运行良好。但当行号变为第三行时,它开始给我错误信息:-

java.lang.IllegalArgumentException:一个单元格中不允许有多个单元格注释

当我在读取单元格值时检测到异常时,我添加注释。下面给出了我用于添加注释的方法:-

private void cellException(Sheet datatypeSheet, CellStyle cellErrorStyle, Row invCurrentRow,
        int invCellNum, String mainMessage)
{

    Cell mainCell = invCurrentRow.getCell(invCellNum);

    if (ExcelUtility.checkIfCellValueAbsent(mainCell))
        mainCell = invCurrentRow.createCell(invCellNum);

    mainCell.setCellStyle(cellErrorStyle);

    Comment mainComment = mainCell.getCellComment();

    if (Util.isNullOrEmpty(mainComment))
        mainComment = datatypeSheet.createDrawingPatriarch().createCellComment(
                new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
    mainComment.setString(new XSSFRichTextString(mainMessage));
    mainCell.setCellComment(mainComment);
    datatypeSheet.autoSizeColumn(mainCell.getColumnIndex());
}
此代码适用于两行。我不明白第三排怎么了

我正在使用XSSFWorkbook,以下是我在springboot项目中添加的依赖项:-

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>

org.apache.poi

您正在使用相同的定位点创建所有单元格注释,该定位点从列索引3、行索引3开始,即
D4
。因此,所有单元格注释都属于
D4
。然后,如果尝试使用同一定位创建单元格注释,并且
D4
中已经有注释,则会导致错误:

java.lang.IllegalArgumentException: Multiple cell comments in one cell are not allowed, cell: D4
不要对每个单元格注释使用不同的achor:

...
  if (mainComment == null)
   mainComment = datatypeSheet.createDrawingPatriarch().createCellComment(
                  //new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
                  new XSSFClientAnchor(0, 0, 0, 0, mainCell.getColumnIndex(), mainCell.getRowIndex(), mainCell.getColumnIndex()+2, mainCell.getRowIndex()+3));
...

您正在使用相同的定位点创建所有单元格注释,该定位点从列索引3、行索引3开始,即
D4
。因此,所有单元格注释都属于
D4
。然后,如果尝试使用同一定位创建单元格注释,并且
D4
中已经有注释,则会导致错误:

java.lang.IllegalArgumentException: Multiple cell comments in one cell are not allowed, cell: D4
不要对每个单元格注释使用不同的achor:

...
  if (mainComment == null)
   mainComment = datatypeSheet.createDrawingPatriarch().createCellComment(
                  //new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
                  new XSSFClientAnchor(0, 0, 0, 0, mainCell.getColumnIndex(), mainCell.getRowIndex(), mainCell.getColumnIndex()+2, mainCell.getRowIndex()+3));
...

我没有注意到这个错误。你是对的。后来我意识到,我无法只在D4单元格中添加注释。非常感谢。我没有注意到这个错误。你是对的。后来我意识到,我无法只在D4单元格中添加注释。谢谢。