我必须在Excel文件中写入一些数据。但是当我打开包含此数据的 Excel 文件时,会出现警告:
`The file you are trying to open, 'name.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a thrusted source before opening the file. Do you want to open the file now?`
我使用的是 Microsoft Excel 2010。 当我创建 HttpServletResponse 响应时,我添加了有关 header 和内容类型的信息:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", some info about file name + ".xls\"");
我还尝试将另一个 header 设置为: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
并将文件格式设置为 .xlsx
,但它也没有'没有帮助,因为警告仍然出现。
当我尝试使用 .csv 或 .xml 格式时,没有出现此问题,因为没有出现警告。是否有可能以编程方式消除此警告?
最佳答案
The file you are trying to open, 'name.xls', is in a different format than specified by the file extension...
这是加载带有 xls
扩展名的 xlsx
文件时的标准 Excel 警告。没有办法以编程方式避免它。您必须解决根本的不匹配问题。
调试:
保存文件的本地副本,将扩展名更改为 xlsx,然后查看它是否会在没有警告的情况下加载到 Excel 中。
如果是,则调整您的 ContentType header 以适应。
如果不是,则文件可能采用其他格式。找出该格式是什么,然后再次调整标题/文件名以适应。
另请参阅我的回答 here关于Extension Hardening .
关于JAVA - 保存Excel(Office 2010)而没有格式警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17968115/