简体   繁体   English

为什么打开.xls文件时显示错误消息

[英]Why showing error message while opening .xls file

In my asp.net, C# application we are generating and downloading .xls file. 在我的asp.net C#应用程序中,我们正在生成和下载.xls文件。 But when I'm trying to open, it's giving a message 但是当我尝试打开时,它在传达信息

"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. 我将文件扩展名更改为.xlsx,仍然是相同的消息。 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 . 我在IIS管理器中添加了.xlsx MIME类型扩展名,其MIME类型为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet Still it's showing the same message. 仍然显示相同的消息。 Please suggest how can I get rid off it. 请提出如何摆脱它的建议。

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. 看来,这与您的应用程序将xls文件提供给浏览器的方式无关。 It's an issue with Office 2007 excess of security. Office 2007安全性过高是一个问题。

Microsoft's KB article 948615 explains it: 微软的知识库文章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. 唯一的解决方案是将en条目添加到客户端的注册表中。 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. 我知道,对于可能有数百名用户的Web应用程序来说,这并不是一个真正的解决方案,但是至少您可以在页面上添加一条注释,告知如何解决该烦人的警告。

Do you completely create the xls file or do you copy and fill a template xls file ? 您是完全创建xls文件还是复制并填充模板xls文件? An incorrect format template file may cause the problem. 不正确的格式模板文件可能会导致此问题。

Also, what provider do you use to fill your file ? 另外,您使用什么提供者来填充文件? - Microsoft.ACE.OLEDB.12.0 for xlsx/xlsm ? -用于xlsx / xlsm的Microsoft.ACE.OLEDB.12.0吗? - Microsoft.Jet.OLEDB.4.0 for xls ? -用于xls的Microsoft.Jet.OLEDB.4.0?

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();

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. 该警报是Excel 2007中的一项新的安全功能,称为扩展强化,可确保打开的文件内容与尝试打开文件的shell命令中指定的扩展名类型匹配。 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. 当前设计不允许您使用Excel在Web站点中打开HTML内容,除非URL的扩展名是.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. 因此,返回HTML并将MIME类型设置为XLS之类的ASP页,试图强制HTML在Excel而不是Web浏览器中打开(如预期的那样),因为内容与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

Not sure how you are exporting data to excel. 不确定如何将数据导出到excel。 Setting the DisplayAlerts to false might help. 将DisplayAlerts设置为false可能会有所帮助。

xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;

I use interop DLL. 我使用互操作DLL。 The namespace would be Microsoft.Office.Interop.Excel . 命名空间为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 请参阅以下链接中有关如何添加引用的Chen Chen的答案: http : //social.msdn.microsoft.com/Forums/en/netfxsetup/thread/c9e83756-4ae2-4ed4-b154-1537f3bb3a22

The following link might also help: 以下链接也可能有帮助:

http://www.dotnetperls.com/excel http://www.dotnetperls.com/excel

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 .xlsx format. 您必须将Excel文件导出为.xlsx格式。 From my experience it will then support it in all versions. 根据我的经验,它将支持所有版本。

Add below DLLs into bin folder 将以下DLL添加到bin文件夹中

  • ClosedXML.dll 关闭的XML.dll
  • DocumentFormat.OpenXml.dll DocumentFormat.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();
                }
     }

You are using .xls in filename and .xlsx in the MIME type. 您在文件名中使用.xls,在MIME类型中使用.xlsx。 Try with filname.xlsx. 尝试使用filname.xlsx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM