<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-HorizontalAlign="left" > <Columns> <asp:TemplateField HeaderText="标题" ItemStyle-HorizontalAlign="left"> <ItemTemplate > <%# Eval("name")%> </ItemTemplate> <ItemStyle HorizontalAlign="Left" Width="300px"></ItemStyle> </asp:TemplateField> <asp:TemplateField HeaderText="网址" ItemStyle-HorizontalAlign="left"> <ItemTemplate> /<%#Eval("")%>/p_<%#Eval("pId")%> </ItemTemplate> <ItemStyle HorizontalAlign="Left" Width="400px"></ItemStyle> </asp:TemplateField> </Columns> <HeaderStyle HorizontalAlign="left"/> </asp:GridView> protected void btndc_click(object sender, EventArgs e) { int num = 1000; if (txtdc.Text != "") { num = Convert.ToInt32(txtdc.Text); } GridView1.AllowPaging = false; GridView1.DataSource = DB.GetDataTable(参数); GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xls"); Response.ContentType = "application/vnd.ms-excel"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.Charset = ""; this.EnableViewState = false; StringWriter oStringWriter = new StringWriter(); HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter); //输出到excel GridView1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { if (!control.GetType().Equals(GridView1.GetType())) { base.VerifyRenderingInServerForm(control); } }