gridview 导出成excel或word文件
时间:2011-04-13 来源:缘来
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls"); //ms-doc 导出成word
}
private void Export(string FileType, string FileName)
{
//以下三行可选,如果没有的话导出的只是当前页数据,没有其他页数据
// GridView1.AllowPaging = false;
//GridView1.AllowSorting = false;
// gridviewdatabind(); //这里是你绑定gridview的方法
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
{
// Confirms that an HtmlForm control is rendered for
}