Excel代,内容不可读

Excel generation, unreadable content

我正在使用 Apache POI 3.12SXSSF 工作簿)来生成 .xlsx 文件。 问题是我正在生成,当我打开文件时收到一条错误消息:

Excel found unreadable content in file.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

点击 Yes 后,文件打开,我收到此通知

Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded. Removed Records: Comments from /xl/comments1.xml part (Comments) Repaired Records: Comments from /xl/comments1.xml part (Comments)

之后,我解压缩 excel 文件并检查 comments1.xml。我所有的评论都存在。所有 216 个。

生成评论的代码部分如下

String comment = _propertiesHolder.getComment();
String commentAuthor = _propertiesHolder.getCommentAuthor();
if(comment != null)
{
    int colIndex = cell.getColumnIndex();
    int rowIndex = cell.getRowIndex();
    CreationHelper helper = _workbook.getCreationHelper();
    ClientAnchor anchor = helper.createClientAnchor();
    anchor.setCol1(colIndex);
    anchor.setCol2(colIndex + 1);
    anchor.setRow1(rowIndex);
    anchor.setRow2(rowIndex + 3);

    // Create the comment and set the text+author
    Comment cellComment = _drawingPatriarch.createCellComment(anchor);
    if(commentAuthor != null)
    {
        cellComment.setAuthor(commentAuthor);
        RichTextString rs = helper.createRichTextString(commentAuthor + ": " + comment);
        cellComment.setString(rs);
    }
    else 
    {
        cellComment.setString(helper.createRichTextString(comment));
    }

    cellComment.setRow(rowIndex);
    cellComment.setColumn(colIndex);

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

你知道这个问题的原因是什么吗?虽然没有丢失信息,但显然有问题,我想修复它。从数据库中检索评论(varchar 数据类型)。最大的评论是 138 个字符长。

更新

有件事我忘了说。我也使用 hssf 实现 运行 相同的提取,并且没有出现错误。可以安全地假设数据不是问题所在。

好的,我找到问题了。是和作者一起的。

问题出在这一行cellComment.setAuthor(commentAuthor);

如果对于一条评论我们设置

cellComment.setAuthor("test")

然后在另一条评论中我们设置

cellComment.setAuthor("test ")

打开文件时会显示错误。注意空格。解决办法是先trim作者字符串再设置。