gpt4 book ai didi

java - 使用 SXSSF(APACHE POI) 并添加注释不会生成正确的 Excel 文件

转载 作者:行者123 更新时间:2023-12-03 00:25:44 27 4
gpt4 key购买 nike

我在使用 SXSSF poi api 生成有效的 xlsx 文件时遇到问题。如果我使用以下代码,则会生成正确的 Excel 文件:工作簿 wb = new XSSFWorkbook();但如果我使用失败:SXSSFWorkbook wb = 新SXSSFWorkbook(100);

错误:已删除记录:/xl/comments1.xml 部分中的注释(注释)

请帮我理解代码有什么问题。我希望使用 SXSSF api 生成带有单元格注释的 Excel 文件。

实际代码:

    public static void main(String[] args) throws Exception {
// Workbook wb = new XSSFWorkbook();
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sh = wb.createSheet();
for(int rownum = 0; rownum < 1000; rownum++){
Row row = sh.createRow(rownum);
for(int cellnum = 0; cellnum < 10; cellnum++){
Cell cell = row.createCell(cellnum);
String address = new CellReference(cell).formatAsString();
cell.setCellValue(address);
setCellComment(cell,address);
}
}

FileOutputStream out = new FileOutputStream("comments.xlsx");
wb.write(out);
out.close();
}

protected static void setCellComment(Cell cell, String message) {
Drawing drawing = cell.getSheet().createDrawingPatriarch();
CreationHelper factory = cell.getSheet().getWorkbook()
.getCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 1);
anchor.setRow1(cell.getRowIndex());
anchor.setRow2(cell.getRowIndex() + 1);
anchor.setDx1(100);
anchor.setDx2(100);
anchor.setDy1(100);
anchor.setDy2(100);

// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString(message);
comment.setString(str);
comment.setAuthor("Apache POI");
// Assign the comment to the cell
cell.setCellComment(comment);
}

最佳答案

我遇到了同样的问题,并通过在Comment上设置行和列属性来解决它。

按如下方式修改示例中的 setCellComment() 方法应该可以解决问题。

protected static void setCellComment(Cell cell, String message) {

...

// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString(message);
comment.setString(str);
comment.setAuthor("Apache POI");

// Set the row and column here
comment.setRow(cell.getRowIndex());
comment.setColumn(cell.getColumnIndex());

// Assign the comment to the cell
cell.setCellComment(comment);
}

我通过查看 setCellComment() 的来源发现了这个解决方案XSSFCell 类中的方法。 SXSSFCell中对应的方法不会设置行或列。

关于java - 使用 SXSSF(APACHE POI) 并添加注释不会生成正确的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28169011/

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