//导出excel按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "农田环境数据.xls");
}
//导出word按钮事件
protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "农田环境数据.doc");
//Export("application/ms-word", "农田环境数据.doc");//都可以
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); Response.Write(tw.ToString());
Response.End();
}
// 此方法必重写,否则会出错
public override void VerifyRenderingInServerForm(Control control)
{
}