我使用了Asp.net webform c#。使用时将网格数据导出到excel,我收到以下错误消息 “文件格式和传输模式的扩展名.xls不匹配。文件可能已损坏或不安全。除非您信任其来源,否则不要打开它。无论如何都要打开它”
点击“是”后数据显示正常。
代码:
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvmodeoftransport.AllowPaging = false;
for (int i = 0; i < gvmodeoftransport.HeaderRow.Cells.Count; i++)
{
gvmodeoftransport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvmodeoftransport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();