日期:2014-05-18 浏览次数:20436 次
public static void ExportExcel(GridView gridview,string filename,DataBindGridView databindgridview) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); //System.Text.Encoding.GetEncoding("GB2312") //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls\""); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //System.Text.Encoding.GetEncoding("GB2312");//System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 //System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); gridview.EnableViewState = false; //this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);//:中国 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); databindgridview(); gridview.RenderControl(oHtmlTextWriter); HttpContext.Current.Response.Output.Write(oStringWriter.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }
//System.Text.Encoding.GetEncoding("GB2312") //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls\""); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
------解决方案--------------------