C# 为什么打开 .xls 文件时显示错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9799726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why showing error message while opening .xls file
提问by Sukanya
In my asp.net, C# application we are generating and downloading .xls file. But when I'm trying to open, it's giving a message
在我的 asp.net、C# 应用程序中,我们正在生成和下载 .xls 文件。但是当我尝试打开时,它会发出一条消息
"The file you are trying to open, 'filename.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
“您尝试打开的文件 'filename.xls' 的格式与文件扩展名指定的格式不同。在打开文件之前,请确认该文件没有损坏并且来自受信任的来源。您要打开吗?现在文件?”
If I press 'Yes' it's opening. I changed the file extension to .xlsx, still same message. Can anyone tell me why this is happening? I've added .xlsx MIME type extension in IIS manager with MIME Type as application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. Still it's showing the same message. Please suggest how can I get rid off it.
如果我按“是”,它就会打开。我将文件扩展名更改为 .xlsx,仍然是相同的消息。谁能告诉我为什么会这样?我在 IIS 管理器中添加了 .xlsx MIME 类型扩展名,MIME 类型为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. 它仍然显示相同的消息。请建议我如何摆脱它。
采纳答案by jbl
Do you completely create the xls file or do you copy and fill a template xls file ? An incorrect format template file may cause the problem.
您是完全创建 xls 文件还是复制并填充模板 xls 文件?不正确的格式模板文件可能会导致此问题。
Also, what provider do you use to fill your file ? - Microsoft.ACE.OLEDB.12.0 for xlsx/xlsm ? - Microsoft.Jet.OLEDB.4.0 for xls ?
另外,您使用什么提供程序来填充您的文件?- xlsx/xlsm 的 Microsoft.ACE.OLEDB.12.0 ?- Microsoft.Jet.OLEDB.4.0 for xls?
An incorrect provider/extension combination may cause the problem.
不正确的提供程序/扩展程序组合可能会导致问题。
According to your comment, here is a part of code where I have done that in the past : ( commented some of the lines as I don't remember why they where useful )
根据您的评论,这是我过去做过的代码的一部分:(评论了一些行,因为我不记得为什么它们有用)
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentEncoding = Encoding.Default;
//Response.Charset="";
Response.AddHeader("Content-Disposition: ",
String.Format(@"attachment; filename={0}",myfileName));
//EnableViewState = false;
Response.Write(myFileContentAsString);
Response.Flush();
Response.Close();
回答by Mujtaba Hassan
You are using .xls in filename and .xlsx in the MIME type. Try with filname.xlsx.
您在文件名中使用 .xls,在 MIME 类型中使用 .xlsx。尝试使用文件名.xlsx。
回答by Manoj
Not sure how you are exporting data to excel. Setting the DisplayAlerts to false might help.
不确定您如何将数据导出到 Excel。将 DisplayAlerts 设置为 false 可能会有所帮助。
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
I use interop DLL. The namespace would be Microsoft.Office.Interop.Excel.
See Feng Chen's answer in the following link on how to add the reference: http://social.msdn.microsoft.com/Forums/en/netfxsetup/thread/c9e83756-4ae2-4ed4-b154-1537f3bb3a22
我使用互操作 DLL。命名空间将是Microsoft.Office.Interop.Excel. 有关如何添加参考的信息,请参阅以下链接中冯晨的回答:http: //social.msdn.microsoft.com/Forums/en/netfxsetup/thread/c9e83756-4ae2-4ed4-b154-1537f3bb3a22
The following link might also help:
以下链接也可能有帮助:
回答by Adrian Platte
This one might be an old post, but I've been searching answers for the same issue, and thought it could be useful. It looks it's not related to the way your application gives the xls file to the browser. It's an issue with Office 2007 excess of security.
这可能是一篇旧帖子,但我一直在寻找同一问题的答案,并认为它可能有用。看起来它与您的应用程序将 xls 文件提供给浏览器的方式无关。这是 Office 2007 过度安全的问题。
Microsoft's KB article 948615explains it:
Microsoft 的知识库文章 948615 对此进行了解释:
When you open a file in Excel 2007, you receive a warning that the file format differs from the format that the file name extension specifies.
当您在 Excel 2007 中打开文件时,您会收到一条警告,指出文件格式与文件扩展名指定的格式不同。
The only solution is to add en entry to the client's registry. I know that's not a real solution for a web application maybe with hundreds of users, but at least you can add a note to the page telling how to fix that annoying warning.
唯一的解决方案是将 en 条目添加到客户端的注册表中。我知道对于可能有数百个用户的 Web 应用程序来说,这不是一个真正的解决方案,但至少您可以在页面上添加一条注释,告诉您如何修复那个烦人的警告。
回答by Tipper
The alert is a new security feature in Excel 2007 called Extension Hardening, which ensures that the file content being opened matches the extension type specified in the shell command that is attempting to open the file. The current design does not allow you to open HTML content from a web site in Excel unless the extension of the URL is .HTM/.HTML/.MHT/.MHTML. So ASP pages that return HTML and set the MIME type to something like XLS to try to force the HTML to open in Excel instead of the web browser (as expected) will always get the security alert since the content does not match the MIME type.
该警报是 Excel 2007 中称为扩展强化的新安全功能,可确保打开的文件内容与尝试打开文件的 shell 命令中指定的扩展类型相匹配。当前设计不允许您在 Excel 中从网站打开 HTML 内容,除非 URL 的扩展名为 .HTM/.HTML/.MHT/.MHTML。因此,返回 HTML 并将 MIME 类型设置为类似 XLS 以尝试强制 HTML 在 Excel 中而不是 Web 浏览器中打开的 ASP 页面(如预期)将始终收到安全警报,因为内容与 MIME 类型不匹配。
http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
回答by Jitendra G2
The file extension .xls (Version 2003) supports the HTML layout whereas office 2007 and above versions do not support it.
文件扩展名 .xls(2003 版)支持 HTML 布局,而 office 2007 及以上版本不支持。
You must export the excel file to .xlsxformat. From my experience it will then support it in all versions.
您必须将 excel 文件导出为 .xlsx格式。根据我的经验,它将在所有版本中支持它。
Add below DLLs into bin folder
将以下 DLL 添加到 bin 文件夹中
- ClosedXML.dll
- DocumentFormat.OpenXml.dll
- 关闭的XML文件
- 文档格式.OpenXML.dll
Code to export file to .xlsx
将文件导出为 .xlsx 的代码
DataTable dt = new DataTable();
//Create column and inser rows
using (XLWorkbook wb = new XLWorkbook())
{
var ws = wb.Worksheets.Add(dt, Sheetname);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + p_FileName + ".xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}

