我正在将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();
}
}
答案 0 :(得分:2)
这是因为Excel知道这不是一个真正的Excel文件,即使你用.xls扩展名命名它。在过去,为了避免这种警告,我使用了Microsoft.Office.Interop.Excel引用来构建我的输出文件。然后,当你完成后,你将拥有一个合法的Excel文件。
<强> Microsoft.Office.Interop.Excel 强>
编辑:我已经用Google搜索并发现了Microsoft的 this suggestion ,但它要求您破解客户端计算机的注册表(可能不可行)。