aspx页面代码
<div><asp:GridView ID="GridView1" runat="server"></asp:GridView><asp:Button ID="Button1" runat="server" Text="导出到Excel" OnClick="Button1_Click" /><!--datalist的导出--><asp:DataList ID="DataList1" runat="server"><ItemTemplate><table><tr><td><%#Eval("itemId")%></td><td><%#Eval("title")%></td><td><%#Eval("price")%></td><td><%#Eval("imageUrl")%></td> </tr></table></ItemTemplate></asp:DataList><asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /><!--detailsView的导出--><asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"></asp:DetailsView><asp:Button ID="Button3" runat="server" Text="detailview的导出" οnclick="Button3_Click" /><!--formview的导出--><asp:FormView ID="FormView1" runat="server"><ItemTemplate><table><tr><td><%#Eval("itemId")%></td><td><%#Eval("title")%></td><td><%#Eval("price")%></td><td><%#Eval("imageUrl")%></td> </tr></table></ItemTemplate> </asp:FormView><asp:Button ID="Button4" runat="server" Text="FormView的导出" οnclick="Button4_Click" /><!--repeater控件导出--><asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><table><tr><td><%#Eval("itemId")%></td><td><%#Eval("title")%></td><td><%#Eval("price")%></td><td><%#Eval("imageUrl")%></td> </tr></table></ItemTemplate></asp:Repeater><asp:Button ID="Button5" runat="server" Text="repeater的导出" οnclick="Button5_Click" /><!--listview控件导出--><asp:ListView ID="ListView1" runat="server"><LayoutTemplate> <table cellpadding="2" runat="server" id="tblDepartments" width="640px" cellspacing="0"> <tr id="itemPlaceholder" runat="server"></tr></table> </LayoutTemplate><ItemTemplate><tr id="Tr1" runat="server"> <td><asp:Label ID="IDLabel" runat="server" Text='<%#Eval("itemId") %>' /></td><td><asp:Label ID="NameLabel" runat="server" Text='<%#Eval("title") %>' /></td><td><asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("price") %>' /></td></tr></ItemTemplate></asp:ListView><asp:Button ID="Button6" runat="server" Text="listview导出" οnclick="Button6_Click" /></div>
后台代码:
using System;using System.IO;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Company.DAL;using System.Text;public partial class dataToExcel : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){getData();}}public void getData(){ItemDAL iadl = new ItemDAL();GridView1.DataSource= iadl.GetDataSet().Tables[0];GridView1.DataBind();DataList1.DataSource = iadl.GetDataSet().Tables[0];DataList1.DataBind();DetailsView1.DataSource = iadl.GetDataSet().Tables[0];DetailsView1.DataBind();FormView1.DataSource = iadl.GetDataSet().Tables[0];FormView1.DataBind();Repeater1.DataSource = iadl.GetDataSet().Tables[0];Repeater1.DataBind();ListView1.DataSource = iadl.GetDataSet().Tables[0];ListView1.DataBind();}protected void Button1_Click(object sender, EventArgs e){DataToExcel(this.GridView1,"订单");}/// <summary>/// 导出到Excel/// </summary>/// <param name="gv">gridview类型</param>/// <param name="ExcName">导出的excel文件名字</param>public void DataToExcel(GridView gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}//如果没有重写方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 public override void VerifyRenderingInServerForm(Control control){//base.VerifyRenderingInServerForm(control);}#region datalist的导出 public void DataToExcel(DataList gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}protected void Button2_Click(object sender, EventArgs e){DataToExcel(this.DataList1, "订单2");}#endregion#region detailview的导出 public void DataToExcel(DetailsView gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}protected void Button3_Click(object sender, EventArgs e){DataToExcel(this.DetailsView1, "订单3");}#endregion#region formview的导出public void DataToExcel(FormView gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}protected void Button4_Click(object sender, EventArgs e){DataToExcel(this.FormView1, "订单4");}#endregion#region repeater的导出 public void DataToExcel(Repeater gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}protected void Button5_Click(object sender, EventArgs e){DataToExcel(this.Repeater1, "订单5");}#endregionpublic void DataToExcel(ListView gv, string ExcName){Response.Clear();//Response.Charset = "UTF-8";//Response.Charset = "GB2312"; 可以不用设置编码格式Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExcName + ".xls");//Response.ContentEncoding = System.Text.Encoding.Default;//Response.ContentEncoding = Encoding.UTF8;HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";StringWriter sw = new StringWriter();HtmlTextWriter htw = new HtmlTextWriter(sw);//gv.HeaderStyle.Font.Bold = true;// gv.EnableViewState = false; gv.RenderControl(htw);Response.Write(sw.ToString());Response.End();}protected void Button6_Click(object sender, EventArgs e){DataToExcel(this.ListView1, "订单6");}}
本文附件下载: Company.rar (1.7 MB)
已有0人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
—软件人才免语言低担保 赴美带薪读研!—