DataSet ds = dao.CustomSelect("SQL语句"); if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0) { return; } Response.Clear(); //Encoding pageEncode = Encoding.GetEncoding(PageEncode); HttpContext.Current.Response.Charset = "gb2312"; Response.ContentType = "application/vnd-excel";//"application/vnd.ms-excel"; //Response.ContentType = "application/x-octet-stream";//"application/vnd.ms-excel"; //Response.ContentType = "text/csv";//"application/vnd.ms-excel"; Response.AppendHeader("Content-Disposition", "attachment;filename=Order.xls"); System.IO.StringWriter oSW = new System.IO.StringWriter(); HtmlTextWriter oHW = new HtmlTextWriter(oSW); DataGrid dg = new DataGrid(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(oHW); Response.Write(oSW.ToString()); Response.Flush(); Response.Close();