C# 打开导出的excel文件时出现错误消息

C# 打开导出的excel文件时出现错误消息,c#,asp.net,C#,Asp.net,我正在用JS中的一个普通onRequestStart函数将网格导出到excel <script type="text/javascript"> function onRequestStart(sender, args) { if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 || args.get_eventTar

我正在用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>
到目前为止,一切都很正常,但在下载完文件并在excel中打开后,出现了一条奇怪的消息,有没有办法禁用此消息

谢谢你的帮助和快速回答


PS:如果您需要更多信息,请随时询问

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


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

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

编辑

或如其他答案中所述,.csv。同样的概念也适用


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

此错误是因为使用Mime类型而不是Microsoft.Office.Interop.Excel创建Excel文件

此错误的原因:


解释来源:

我认为问题在于,这个网格组件正在将其导出为html,只需添加一个xlsx标记,或者其他任何东西。2010年的excel有一种新的机制,可以保护excel打开未知文件。我认为就我自己而言,这个问题没有解决方案

我目前正在用c编写一个程序。在c之外,如果您只是试图在windows上从文件系统打开文件,您可以修改注册表以忽略它


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

sry但是你在哪里看到的,你能解释一下吗=?你不是这么说的吗:然后我调用Grid\u itemcand检查它是否是excel导出单词或CSV,然后我调用我的方法doExport?是的,因此它看到文件不是excelcsv@Mingebag你真的读过你贴的东西吗?您发布了一张图片,显示您正在将文件导出为XLS,其中包含完整的文件名,但您说您正在将其导出为CSV…@Mingebag我已经给您提供了一个解决方案:导出文件时,将其命名为file_2013_或_which.CSV,问题将消失。抱歉,注释已删除,我希望你能找到你需要的帮助提示另存为XLSX而不是XLS:@JMK谢谢这里也是我说我将文件另存为XLS的地方?在你的截图中,@ZorleQ在我写评论之前给出了同样的答案。做得很好@JMK,我以前从未使用过UserGrid,但我在其他导出到Excel的工具中多次看到这个错误。出于某些原因,开发人员总是弄乱这种格式。@JMK尝试了类似于this.UserGrid.ExportSettings.FileName=String.FormatYearReport{0}{1}、this.selectedYear、this.rcbDepartments.SelectedValue==XLS?CSV:this.rcbDepartments.SelectedValue的方法‌​; 但不知怎么的,它不起作用。你能给我提供解决方案吗?试着直接在文件名后面加上扩展名。也许这会迫使它。尝试使用CVS和XLSX this.UserGrid.ExportSettings.FileName=String.FormatYearReport_{0}{1}.XLSX,this.selectedYear,this.rcbDepartments.SelectedValue;没有任何更改,只是收到了相同的消息,带有不同的名称YearReport_2013_u.XLSX.xlsSeems,就像该组件正在使用其默认扩展名一样。除非您找到一个选项,允许覆盖该选项,否则您唯一要做的就是@MCraft所说的——在导出文件后附加.xlsx。有人同意吗?
 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);
        }
    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.