我正在使用 Apache POI 3.12
( SXSSF
工作簿)来生成 .xlsx
文件 . 问题是我收到错误信息:'m doing the generation and when I open the file I'm
Excel在file.xlsx中找到了不可读的内容 . 你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击“是” .
单击 Yes
后,文件将打开,我收到此通知
Excel完成了文件级验证和修复 . 本工作簿的某些部分可能已被修复或丢弃 . 已移除的记录:来自/xl/comments1.xml的注释(注释)已修复的记录:来自/xl/comments1.xml的注释(注释)
之后,我解压缩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个字符长 .
Update
我忘了提的东西 . 我也使用 hssf
实现运行相同的提取,并且不存在任何错误 . 可以安全地假设数据不是问题 .
1 回答
好的我发现了问题 . 这是与作者 .
问题是这一行
cellComment.setAuthor(commentAuthor);
.如果我们设置一个评论
cellComment.setAuthor("test")
然后在另一个评论中我们设定
cellComment.setAuthor("test ")
打开文件时会显示错误 . 记住空白 . 解决方案是在设置之前修剪作者字符串 .