protected void BtnExport_Click(object sender, EventArgs e) //导出代码
{
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.AddHeader("content-Disposition", "attachment;filename=FileName.xls");//
FileName.xls 为导出的文件名字
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//解决汉字乱码
Response.ContentType = "application/vnd.xls";
// Response.ContentType = "application/ms-excel"; //这两种均可保存为 excel
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); //
解决汉字乱码
this.EnableViewState = false;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.AllowPaging = false; //解决 GridView 分页导出 excel 问题
BindData(); //这是连接数据库绑定函数,可自己写
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridView1.AllowPaging = true;
}
评论6