gpt4 book ai didi

java - Apache POI : Multiple cell comments in one cell are not allowed

转载 作者:行者123 更新时间:2023-12-02 00:57:16 24 4
gpt4 key购买 nike

我正在尝试向单元格添加注释。该代码适用于 2 行。但是当行号转到第三时,它开始给我错误:-

java.lang.IllegalArgumentException: Multiple cell comments in one cell are not allowed

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

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>

我尝试寻找答案,发现了一个我认为有用的答案,但事实并非如此:-

Multiple comments apache poi

最佳答案

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

java.lang.IllegalArgumentException: Multiple cell comments in one cell are not allowed, cell: D4

为每个单元格注释使用不同的 anchor :

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

关于java - Apache POI : Multiple cell comments in one cell are not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61200929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com