具体的实现代码。
1
protected void Button1_Click(object sender, EventArgs e)2

{3
//Gridview绑定数据4
BindData();5

Response.Buffer = true;/**////定义文档类型、字符编码 6
Response.Clear();7
HttpContext.Current.Response.Charset = "UTF-8";8
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;9
HttpContext.Current.Response.ContentType = "application/ms-excel";10

/**////输出的文件11
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "Old.xls");12

/**////定义一个输入流13
System.IO.StringWriter tw = new System.IO.StringWriter();14
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);15

/**////将目标数据绑定到输入流输出 16
DataGrid1.RenderControl(hw);17
HttpContext.Current.Response.Write(tw.ToString());18
HttpContext.Current.Response.End();19
}