//导出Excel操作
protected void btnToExcel_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;//清除分页
GridView1.AllowSorting = false;//清除排序
BindData();//绑定GridView1数据源的函数
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "GB2312";//如果导出乱码改为 UFT-8
//假定导出的文件名为FileName.xls
Response.AppendHeader("Content-Disposition", "attachment;filename=WebInfo.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");<span style="font-family: Arial, Helvetica, sans-serif;">//如果导出乱码改为 UFT-8</span>
//设置导出文件的格式
Response.ContentType = "application/vnd.ms-excel";
//关闭ViewState
EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
GridView1.RenderControl(textWriter);
//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.Flush();
Response.End();
GridView1.AllowSorting = true;//恢复分页
GridView1.AllowPaging = true;//恢复排序
BindData();//再次绑定
}
//数据的绑定方法
public void BindData()
{
string webname = ((TextBox)search).Text.ToString().Trim();
string ddlUser = DropDownList1.SelectedItem.ToString();
DataSet ds = UsersBLL.SearchList(webname, ddlUser);
GridView1.DataSource = ds;
GridView1.DataBind();
}
GridView导出Excel操作(有分页基础上)
最新推荐文章于 2021-06-15 21:08:23 发布