protectedvoid Export_Click(object sender, EventArgs e)
{
test("application/ms-excel", "总公司管理企业总体情况统计表.xls"); //ms-doc导出成word
}
private void test(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();
}
// 如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Controlcontrol)
{
// Confirms that an HtmlForm control is renderedfor
}