DataGrid或GridView 生成Excel后,打开这个Excel会弹出"您尝试打开的文件的格式与文件扩展名指定的格式不一致"
导出Excel的代码很普通,网络上大致相同:
private void ToExcel(DataGrid grid, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
grid.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
导出Excel打开时,会弹出提示"您尝试打开的文件的格式与文件扩展名指定的格式不一致" ,有没有什么办法可以解决这个问题,不要是修改注册表之类
估计DataGrid转成Excel是被保存成了html格式的Excel文件,有什么办法可以改变成正常的Excel格式呢