导出的EXCEL是乱码,改网页编码无效,大家帮下,谢谢
protected void Button2_Click(object sender, EventArgs e)
{
toexcel(ltPage);
}
protected void toexcel(Control ctl)
{
/*HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=baobiao.xls");
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();*/
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.ContentEncoding = System.Text.Encoding.UTF8;
ctl.RenderControl(oHtmlTextWriter);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();