我正在将对象列表导出到 excel 文件,excel 文件已成功下载,但是当我打开此 excel 文件时,它显示一条警告消息
您尝试打开的文件“myfile.xls”的格式与文件扩展名指定的格式不同,请在打开文件之前验证文件没有损坏并且来自受信任的资源。您现在要打开文件吗?
如果我按是,文件将成功打开。如何避免此警告信息?我的代码如下。
public void ToExcel<T>(HttpResponseBase Response, List<T> data,string FileName )
{
var grid = new System.Web.UI.WebControls.GridView();
grid.DataSource = data;
grid.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}