方法一
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=test.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=big5>");
HttpContext.Current.Response.Write("<head><meta http-equiv=Content-Type content=text/html;charset=big5></head>");
Response.Charset = "big5";
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
中文繁體或簡體出現亂碼
big5編碼改成GBK
可能會出現的錯誤訊息
System.Web.HttpException: '型別 'GridView' 的控制項 'GridView1' 必須置於有 runat=server 的表單標記之中。
解決方式
1.在.cs檔加入public override void VerifyRenderingInServerForm(Control control) { }
2.上述若失敗,在apsx檔中最上方的<%page %>加入EnableEventValidation="false"
留言列表