Excel文件是由文件扩展名指定不同的格式(从Excel的GridView的出口) [英] Excel file is in a different format specified by the file extension (Excel exported from gridview)

查看:251
本文介绍了Excel文件是由文件扩展名指定不同的格式(从Excel的GridView的出口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出口的GridView到Excel文件,它会打开就好了。唯一的一点是,此警告每一个Excel文件打开时总是弹出:

你试图打开&LT文件; >是在比由文件扩展名指定的不同的格式。验证该文件没有损坏,是来自可信来源打开文件之前。要现在打开文件?

code我使用的是:

  HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(内容处置的String.Format(附件;文件名= {0},Single_Raw.xls));
        HttpContext.Current.Response.ContentType =应用程序/ vnd.ms-EXCEL;    使用(StringWriter的SW =新的StringWriter())
    {
        使用(HTW的HtmlTextWriter =新的HtmlTextWriter(SW))
        {
           //一些code            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }


解决方案

这是因为Excel知道这是不是一个真正的Excel文件,即使你用.xls扩展名为它。在过去,为了避免这个警告,我用的Microsoft.Office.Interop.Excel引用打造我的输出文件。然后,你就会有一个合法的Excel文件时,即可大功告成。

的Microsoft.Office.Interop.Excel

编辑:我周围的一派,发现的 这个建议 从Microsoft,但它需要你破解客户端计算机的注册表中(可能是不可行的)。

I'm exporting a gridview to an excel file and it opens just fine. Only thing is, this warning always pops up every time the excel file is opened:

The file you are trying to open < > 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?

Code I'm using:

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Single_Raw.xls"));
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
           // some code

            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }

解决方案

That's because Excel knows this isn't an actual Excel file, even though you've named it with an .xls extension. In the past, to avoid this warning, I've used the Microsoft.Office.Interop.Excel reference to build my output file. Then, you'll have a legitimate Excel file when you're done.

Microsoft.Office.Interop.Excel

Edit: I've googled around and found this suggestion from Microsoft, but it requires you to hack the registry of the client's computer (probably not feasible).

这篇关于Excel文件是由文件扩展名指定不同的格式(从Excel的GridView的出口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆