记一次打开Excel文件错误的排查
记一次打开Excel文件错误的排查
December 5, 2018
在生产环境中,某个结果集导出的excel文件用Microsoft Excel无法打开,看到如下错误:
最开始怀疑是在生成文件的时候,文件损坏掉了,但是重新生成多次,得到的结果都是一样的。在相同的界面,导出其他结果集所生成的文件却都是可以成功打开的,这样就可以排除代码功能方面的问题。如果和代码无关,那么就可能和数据有关系。根据出错提示给出的文件地址“C:\Users\ZHANGZ~1\AppData\Local\Temp\error083200_01.xml”, 打开该临时文件,可以看到下面的信息
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
@namespace html url(http://www.w3.org/1999/xhtml); :root { font:small Verdana; font-weight: bold; padding: 2em; padding-left:4em; } * { display: block; padding-left: 2em; } html|style { display: none; } html|span, html|a { display: inline; padding: 0; font-weight: normal; text-decoration: none; } html|span.block { display: block; } *[html|hidden], span.block[html|hidden] { display: none; } .expand { display: block; } .expand:before { content: '+'; color: red; position: absolute; left: -1em; } .collapse { display: block; } .collapse:before { content: '-'; color: red; position: absolute; left:-1em; }
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error084360_01.xml</logFileName>
<summary>Errors were detected in file 'C:\Users\fieldeng\Downloads\test_zh_142053.xlsx'</summary>
<additionalInfo>
<info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info>
</additionalInfo>
<removedParts>
<removedPart>Replaced Part: /xl/worksheets/sheet1.xml part with XML error. 超出当前范围。 Line 478, column 0.</removedPart>
</removedParts>
</recoveryLog>
由文件内容可以估计是某个字段的值超过长度限制,但是依然无法有效地定位到真正出错的地方。后来发现excel文件实际上就是个zip压缩包,于是改一下扩展名,解压后看到一些目录和文件,一切就豁然开朗了。文件解压后,找到/xl/worksheets/sheet1.xml文件,用文本编辑器打开,跳转到478行,果然看到一个超长的字符串。这个超长的字符串是在代码里面拼接N个字段生成的,结果超出了xml文件能够接受的长度。定位到出错的地方,优化一下代码,就把解决问题了。
最后更新于