using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Entity.DAL; using System.Text; using System.Collections;
namespace Entity { public partial class WebForm3 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); // ht.Add("ID1", "ID1"); ht.Add("UserName", "名称"); ht.Add("Age", "年龄"); ht.Add("Sex", "性别"); ht.Add("tel", "电话"); GridView1.Columns.Clear(); GridView1.AutoGenerateColumns = false; foreach (DictionaryEntry de in ht) { BoundField bf = new BoundField(); bf.DataField = de.Key.ToString(); bf.HeaderText = de.Value.ToString();
GridView1.Columns.Add(bf);
} this.GridView1.DataSource = UsersDAL.GetList(); // this.GridView1.Columns["UserName"]= this.GridView1.DataBind(); Export("application/ms-excel", "导出测试.xls", this.GridView1); }
protected void Button1_Click(object sender, EventArgs e) { Response.Write(Excel.GetExcel()); } public override void VerifyRenderingInServerForm(Control control) { }
//导出到Excel private void Export(string FileType, string FileName,GridView gd) { //以下三行可选,如果没有的话导出的只是当前页数据,没有其他页数据 //gvFieldData.AllowPaging = false; //gvFieldData.AllowSorting = false; //GetFieldInfo(false, "obj_name"); //这里是你绑定gridview的方法 // gd.Columns[0].Visible = false; //导出时隐藏操作列 // gd.BottomPagerRow.Visible = false; //隐藏分页行 //string style = @"<style> .text { mso-number-format:\@; } </script> "; Response.Clear(); Response.Buffer = true; //Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); // 中文 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! //Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); gd.RenderControl(oHtmlTextWriter); //Response.Write(style); Response.Output.Write(oStringWriter.ToString()); // Response.Flush(); Response.End();
} protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) {
}
} }
|