打开导出的Excel文件时出现错误消息

时间:2013-03-19 13:50:53

标签: c# asp.net

我正在使用JS中的普通onRequestStart函数将我的网格导出到excel(这是函数)

 <script type="text/javascript">
            function onRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
                 args.get_eventTarget().indexOf("ExportToWordButton") >= 0 ||
                 args.get_eventTarget().indexOf("ExportToCsvButton") >= 0) {
                    args.set_enableAjax(false);
                }
            }
</script>

然后我调用Grid_ItemCommand检查它是否是excel导出词或csv然后我调用我的方法doExport()

 private void doExport()
        {
            this.UserGrid.ExportSettings.ExportOnlyData = true;
            this.UserGrid.ExportSettings.IgnorePaging = true;
            this.UserGrid.ExportSettings.OpenInNewWindow = true;
            this.UserGrid.ExportSettings.FileName = String.Format("YearReport_{0}_{1}", this.selectedYear, this.rcbDepartments.SelectedValue);
        }

到目前为止,一切都运行良好,但在完成下载文件并在excel中打开时,一个奇怪的&gt; “战争消息”到达是否有解决方案如何禁用此消息? enter image description here

感谢您的帮助和快速回答

PS:如果您需要更多的东西,请随时询问

5 个答案:

答案 0 :(得分:3)

问题是实际文件类型和扩展名之间不匹配,因为您将数据导出为CSV但您告诉Excel它是XLS文件。这是正常的Excel警告。

如果要禁用此功能,请确保导出的文件具有“csv”扩展名。

答案 1 :(得分:2)

将导出文件另存为&#34; .xlsx&#34;文件。当您将Office 2003样式另存为.xlsx或其他方式时,会发生这种情况。

修改

或者如其他答案所述,&#34; .csv&#34;。同样的概念适用。

Excel检测到文件内容与给定的扩展名不匹配,因此错误。

答案 2 :(得分:2)

发生此错误是因为使用Mime Type而不是Microsoft.Office.Interop.Excel创建Excel文件,

此错误的原因:

    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. Because the MIME types listed above are 
associated with the .XLS extension, the file must be in XLS (BIFF8) file format to open 
without this warning prompt.  If the file type is a different format (such as HTML, XML, 
CSV, etc.) the prompt is expected since the file content is different that the extension 
or MIME type. The alert is first seen when opening the file from a URL site directly.  If 
you cancel the alert, the open will fail, but then IE will attempt to download the file 
and open again using a different shell command. Depending on what the file contents is and 
what extension IE gives the file it downloads, you may see the second open attempt 
succeed, or you may see the prompt again but with a different filename in the alert dialog.

    The alert prompt is "by design", but the interaction of the cancel action and IE's 
attempt to open the file again is a known problem under investigation for a future fix. 

说明来源: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

答案 3 :(得分:1)

我认为问题是这个网格组件正在将它导出到html并且只是添加一个xlsx标签或者2010年有这么好的东西有这个新的机制来保护excel打开未知文件我想我自己没有该问题的解决方案..

答案 4 :(得分:0)

我目前正致力于使用c#以编程方式执行此操作。在c#之外,如果您只是想在Windows上从文件系统中打开文件,则可以修改注册表以忽略它。

See it in action on 3v4l.org.

如果我得到它,我会跟进我的程序化解决方案。