0

我正在使用 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 中打开一个奇怪的>“Warring Message”之后,是否有解决方案如何禁用此消息? 在此处输入图像描述

感谢您的帮助和快速答复

PS:如果您需要更多内容,请随时询问

4

5 回答 5

3

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

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

于 2013-03-19T13:54:42.830 回答
2

将导出文件另存为“.xlsx”文件。当您将 Office 2003 样式保存为 .xlsx 或以其他方式保存时,这种情况总是会发生。

编辑

或如另一个答案“.csv”中所述。同样的概念也适用。

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

于 2013-03-19T13:57:08.490 回答
2

发生此错误是因为使用Mime Typenot by创建 Excel 文件Microsoft.Office.Interop.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

于 2013-03-19T13:58:20.490 回答
1

我认为问题在于这个网格组件正在将其导出到 html 并且只需添加一个 xlsx 标记或 2010 年的任何 excel 都有这种新机制可以保护 excel 打开未知文件我认为对于我自己来说没有解决方案问题 ..

于 2013-03-20T10:18:22.553 回答
0

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

https://support.microsoft.com/en-us/help/948615/error-opening-file-the-file-format-differs-from-the-format-that-the-fi

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

于 2017-07-07T23:32:50.320 回答