JAVA - 保存Excel(Office 2010)而没有格式警告

标签 java excel warnings office-2010 httpserver

我必须在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 警告。没有办法以编程方式避免它。您必须解决根本的不匹配问题。

调试:

  1. 保存文件的本地副本,将扩展名更改为 xlsx,然后查看它是否会在没有警告的情况下加载到 Excel 中。

  2. 如果是,则调整您的 ContentType header 以适应。

  3. 如果不是,则文件可能采用其他格式。找出该格式是什么,然后再次调整标题/文件名以适应。

另请参阅我的回答 here关于Extension Hardening .

关于JAVA - 保存Excel(Office 2010)而没有格式警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17968115/

相关文章:

python - 在 Windows 中导入 gensim 模块后出现警告消息

java - 我可以定义自己的 java.util.List 子类吗?

java - Spring Data JPA @Query 注解的问题(SQL 语法错误)

java - 从 URL 读取 Excel 文件

excel - 在excel 2007中删除A列上的空行

c++ - 为什么我应该始终启用编译器警告?

java - SAML DNS 解析问题

java - 如何使光标默认出现在 JTextField 中?

excel - 使用单元格值作为变量在 Excel 中插入方程(方程编辑器)

c# - VS中的奇怪警告