diff --git a/CHANGELOG.md b/CHANGELOG.md
index b99c2a739..c020bb748 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
-# 5.8.16.M1 (2023-03-10)
+# 5.8.16.M1 (2023-03-12)
### 🐣新特性
* 【core 】 改进Calculator.conversion,兼容乘法符号省略写法(issue#2964@Github)
@@ -11,6 +11,7 @@
### 🐞Bug修复
* 【crypto】 修复NoSuchMethodError未捕获问题(issue#2966@Github)
+* 【poi 】 修复SXSSFWorkbook调用setComment时错位的问题(issue#I6MBS5@Gitee)
-------------------------------------------------------------------------------------------------------------
# 5.8.15 (2023-03-09)
diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
index 23a334846..0a6779fec 100644
--- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
+++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
@@ -455,12 +455,18 @@ public class CellUtil {
final CreationHelper factory = wb.getCreationHelper();
if (anchor == null) {
anchor = factory.createClientAnchor();
+ // 默认位置,在注释的单元格的右方
anchor.setCol1(cell.getColumnIndex() + 1);
anchor.setCol2(cell.getColumnIndex() + 3);
anchor.setRow1(cell.getRowIndex());
anchor.setRow2(cell.getRowIndex() + 2);
+ // 自适应
+ anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
}
final Comment comment = drawing.createCellComment(anchor);
+ // https://stackoverflow.com/questions/28169011/using-sxssfapache-poi-and-adding-comment-does-not-generate-proper-excel-file
+ // 修正在XSSFCell中未设置地址导致错位问题
+ comment.setAddress(cell.getAddress());
comment.setString(factory.createRichTextString(commentText));
comment.setAuthor(StrUtil.nullToEmpty(commentAuthor));
cell.setCellComment(comment);
diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/IssueI6MBS5Test.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/IssueI6MBS5Test.java
new file mode 100644
index 000000000..9e661e497
--- /dev/null
+++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/IssueI6MBS5Test.java
@@ -0,0 +1,48 @@
+package cn.hutool.poi.excel;
+
+import cn.hutool.poi.excel.cell.CellUtil;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
+/**
+ * https://gitee.com/dromara/hutool/issues/I6MBS5
+ * 经过测试,发现BigExcelWriter中的comment会错位
+ * 修正方式见: https://stackoverflow.com/questions/28169011/using-sxssfapache-poi-and-adding-comment-does-not-generate-proper-excel-file
+ */
+public class IssueI6MBS5Test {
+
+ @Test
+ @Ignore
+ public void setCommentTest() {
+ final ExcelWriter writer = ExcelUtil.getBigWriter("d:/test/setCommentTest.xlsx");
+ final Cell cell = writer.getOrCreateCell(0, 0);
+ CellUtil.setCellValue(cell, "cellValue");
+ CellUtil.setComment(cell, "commonText", "ascend", null);
+
+ writer.close();
+ }
+
+ @Test
+ @Ignore
+ public void setCommentTest2() {
+ final File file = new File("D:\\test\\CellUtilTest.xlsx");
+ try (final Workbook workbook = WorkbookUtil.createBook(true)) {
+ final Sheet sheet = workbook.createSheet();
+ final Row row = sheet.createRow(0);
+ final Cell cell = row.createCell(0);
+ CellUtil.setCellValue(cell, "cellValue");
+ CellUtil.setComment(cell, "commonText", "ascend", null);
+ workbook.write(Files.newOutputStream(file.toPath()));
+ } catch (final IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}