我在使用SXSSF poi api生成有效的xlsx文件时遇到问题 . 如果我使用以下代码生成正确的excel文件:Workbook wb = new XSSFWorkbook();但如果我使用则失败:SXSSFWorkbook wb = new 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);
}
1 回答
我遇到了同样的问题,并通过在
Comment
上设置行和列属性来解决它 .如下修改示例中的
setCellComment()
方法应该可以解决问题 .我通过查看
XSSFCell
类中setCellComment()方法的来源发现了这个解决方案 .SXSSFCell
中的相应方法未设置行或列 .