Excel文件的格式由文件扩展名指定(Excel从gridview导出)

| 我正在将gridview导出到一个excel文件,它可以很好地打开。唯一的事情是,每次打开excel文件时,此警告总是弹出:
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?
我正在使用的代码:
        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();
        }
    }
    
已邀请:
这是因为Excel知道这不是实际的Excel文件,即使您已将其命名为.xls扩展名。过去,为了避免出现此警告,我使用了Microsoft.Office.Interop.Excel引用来构建我的输出文件。然后,完成后您将拥有一个合法的Excel文件。 Microsoft.Office.Interop.Excel 编辑:我已经四处搜寻,并从Microsoft找到了这个建议,但是它要求您破解客户端计算机的注册表(可能不可行)。     

要回复问题请先登录注册