1 void exportexcel()
2 {
3 DataTable dt = getDataTable();
4 if (dt != null)
5 {
6 Response.Clear();
7 Response.Buffer = true;
8 Response.Charset = "GB2312";
9 Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(exceltitle.Value, System.Text.Encoding.UTF8) + ".xls");
10 Response.ContentEncoding = System.Text.Encoding.UTF8;
11
12 Response.ContentType = "application/ms-excel";
13 this.EnableViewState = false;
14 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
15 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
16
17 gvExport = new System.Web.UI.WebControls.GridView();
18 gvExport.DataSource = dt.DefaultView;
19 gvExport.AllowPaging = false;
20 gvExport.DataBind();
21 gvExport.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
22 // 返回客户端
23 gvExport.RenderControl(oHtmlTextWriter);
24 Response.Write(oStringWriter.ToString());
25 Response.End();
26
27
28 }
29 close();
30 }