我下面的這個方法導出的EXCEL亂碼,請各位幫忙看一下。
public static void ExportExcel(string ExcelName, GridView tempFgrid)
{
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
Page page = new Page();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm form = new HtmlForm();
tempFgrid.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(tempFgrid);
page.RenderControl(htw);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + ExcelName + ".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
請考慮一下文字編碼格式,只要雙方對應起來就可以