日期:2014-05-18 浏览次数:20498 次
public class GridViewToExcel { HttpResponse Response = null; public GridViewToExcel(HttpResponse response) { Response = response; } /// <summary> /// 将GridView里的数据导入到Excel文件中,并另存文档 /// </summary> /// <param name="FileName">文件名称</param> /// <param name="GridViewTeacher">要导出数据的GridView</param> public void ExportToExcel(string FileName, GridView gridview) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = "application/ms-excel";//导出excel文件 //this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); gridview.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } }
------解决方案--------------------