From def6fe2b6037fc15af1015e867a057269b4602d9 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 12 Mar 2023 13:18:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DSXSSFWorkbook=E8=B0=83?= =?UTF-8?q?=E7=94=A8setComment=E6=97=B6=E9=94=99=E4=BD=8D=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../cn/hutool/poi/excel/cell/CellUtil.java | 6 +++ .../cn/hutool/poi/excel/IssueI6MBS5Test.java | 48 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 hutool-poi/src/test/java/cn/hutool/poi/excel/IssueI6MBS5Test.java 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); + } + } +}