日期:2014-05-18 浏览次数:20449 次
public void ExportToExcel(string Filename, DataGrid gridview, Page page) { gridview.EnableViewState = false; gridview.AllowSorting = false; gridview.AllowPaging = false; gridview.HeaderStyle.ForeColor = System.Drawing.Color.Black; DataTable temp = (DataTable)ViewState["view"];//把你前边绑定数据的DataTable暂存ViewState. gridview.DataSource = temp; gridview.DataBind(); page.Response.Clear(); // 防止中文内容为乱码 page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); //可令中文文件名不为乱码 page.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\""); System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gridview.RenderControl(htw); page.Response.Write(sw.ToString()); page.Response.End(); }