GridView中如何实现导出含DropdownList、TextBox,实现分页及导出Excel数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false" CodeFile="DropDownLIst.aspx.cs" Inherits="_20100608_Default" %> 2 3 <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <html xmlns="http://www.w3.org/1999/xhtml"> 8 <head runat="server"> 9 <title></title> 10 </head> 11 <body> 12 <form id="form1" runat="server"> 13 <div> 14 <asp:GridView ID="grd" runat="server" BackColor="White" BorderColor="#E7E7FF" 15 BorderStyle="None" BorderWidth="1px" CellPadding="3" 16 GridLines="Horizontal" AutoGenerateColumns="False"> 17 <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> 18 <Columns> 19 20 21 <asp:BoundField DataField="ID" /> 22 <asp:BoundField DataField="Name" /> 23 <asp:TemplateField> 24 <ItemTemplate> 25 <asp:DropDownList runat="server" ID="ddl" Width="50px" AutoPostBack="true"> 26 <asp:ListItem>1</asp:ListItem> 27 <asp:ListItem Selected="True">2</asp:ListItem> 28 </asp:DropDownList></ItemTemplate> 29 </asp:TemplateField > 30 <asp:TemplateField HeaderText="123"> 31 <ItemTemplate> 32 <asp:TextBox ID="txt" runat="server" ></asp:TextBox></ItemTemplate> 33 </asp:TemplateField> 34 35 </Columns> 36 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> 37 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" /> 38 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> 39 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> 40 <AlternatingRowStyle BackColor="#F7F7F7" /> 41 </asp:GridView> 42 <asp:Button ID="btnDataToExcel" runat="server" Text="导入Excel" Width="166px" 43 onclick="btnDataToExcel_Click" /> 44 <webdiyer:AspNetPager ID="PagerNet" runat="server" PageSize="5" 45 onpagechanged="PagerNet_PageChanged"> 46 </webdiyer:AspNetPager> 47 </div> 48 </form> 49 </body> 50 </html>
后台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public partial class _20100608_Default : System.Web.UI.Page 2 { 3 DataSet ds=DBAccess.ExecuteDataset("Select row_number() over(order by Id) as num, * from person"); 4 protected void Page_Load(object sender, EventArgs e) 5 { 6 if (!IsPostBack) 7 { 8 OnBind(); 9 } 10 } 11 private void OnBind() 12 { 13 PagerNet.RecordCount= 12; 14 DataView tb = ds.Tables[0].DefaultView; 15 tb.RowFilter = "num >= " + PagerNet.StartRecordIndex+" and num <= " +PagerNet.EndRecordIndex ; 16 //grd.DataSource = DBAccess.ExecuteDataset("select * from (Select row_number() over(order by Id) as num, * from xls) n where n.num between " + PagerNet.StartRecordIndex + "and " + PagerNet.EndRecordIndex); 17 grd.DataSource = tb; 18 grd.DataBind(); 19 for (int j = 0; j < grd.Rows.Count; j++) 20 { 21 DropDownList drp = grd.Rows[j].FindControl("ddl") as DropDownList; 22 drp.Items.Insert(0,"AA"); 23 } 24 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void DisableControls(GridView gv) { for (int i = 0; i < gv.Rows.Count; i++) { //dropdownlist DropDownList drp = grd.Rows[i].FindControl("ddl") as DropDownList; Label lb = new Label(); lb.Text = drp.SelectedItem.Text; grd.Rows[i].Cells[2].Controls.Add(lb); grd.Rows[i].Cells[2].Controls.RemoveAt(1); ; //TextBox TextBox txt = grd.Rows[i].FindControl("txt") as TextBox; lb = new Label(); lb.Text = txt.Text; grd.Rows[i].Cells[3].Controls.Add(lb); grd.Rows[i].Cells[3].Controls.RemoveAt(1); ; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void btnDataToExcel_Click(object sender, EventArgs e) 2 { 3 StringWriter objStringWriter = new StringWriter(); 4 HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter); 5 this.grd.AllowPaging = false; 6 grd.DataSource = ds; 7 grd.DataBind(); 8 DisableControls(grd); 9 grd.RenderControl(objHtmlTextWriter); 10 string style = @"<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><style> .text { mso-number-format:\@; } </style></head><body>"; 11 Response.Clear(); 12 Response.Buffer = true; 13 Response.AppendHeader("Content-Disposition", "attachment;filename=123.xls"); 14 Response.ContentType = "application/ms-excel "; 15 Response.Write(style); 16 Response.Write(objStringWriter.ToString()); 17 Response.Write("</body></html>"); 18 Response.End(); 19 OnBind(); 20 } 21 public override void VerifyRenderingInServerForm(System.Web.UI.Control control) 22 { 23 //== 處理 GridView的控制項。 'GridView' 必須置於有 runat=server 的表單標記之中 24 //== http://msdn.microsoft.com/zh-tw/library/system.web.ui.page.verifyrenderinginserverform(VS.80).aspx 25 //—註解:或是在 Web.Config檔案裡面,增加這一行<pages enableEventValidation ="false" ></pages> 就可以免除上面這個事件。 26 } 27 protected void PagerNet_PageChanged(object sender, EventArgs e) 28 { 29 OnBind(); 30 }
自己的项目: |
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <asp:GridView ID="gridView" runat="server" AllowPaging="True" Width="100%" CellPadding="3" 2 OnPageIndexChanging="gridView_PageIndexChanging" BorderWidth="1px" DataKeyNames="payid" 3 OnRowDataBound="gridView_RowDataBound" RowStyle-HorizontalAlign="Center" OnRowCreated="gridView_OnRowCreated" 4 OnRowEditing="gridView_RowEditing" AutoGenerateColumns="False"> 5 <Columns> 6 <%--1.序列号--%> 7 <asp:TemplateField HeaderText="序列号" ShowHeader="False" Visible="false"> 8 <ItemTemplate> 9 <asp:Label ID="lab_Payid" runat="server" Text='<%# Bind("payid") %>' Width="70"></asp:Label> 10 </ItemTemplate> 11 </asp:TemplateField> 12 <%--2.选择--%> 13 <asp:TemplateField ControlStyle-Width="30" HeaderText="选择"> 14 <ItemTemplate> 15 <asp:CheckBox ID="DeleteThis" onclick="javascript:CCA(this);" runat="server" /> 16 </ItemTemplate> 17 <ControlStyle Width="30px"></ControlStyle> 18 </asp:TemplateField> 19 <%-- 3.月份--%> 20 <asp:TemplateField HeaderText="月份" SortExpression="dataMoth"> 21 <ItemTemplate> 22 <% if (this.ddl_YesNo_State.SelectedValue == "未发放") 23 {%> 24 <asp:Label ID="Label1" runat="server" Text='<%# subString((DateTime.Now.ToString("yyyy年MM月")).ToString())%>' 25 Width="70"></asp:Label> 26 <%} 27 else 28 { 29 %> 30 <asp:Label ID="Label4" runat="server" Text='<%# subString((Eval("dataMoth")).ToString())%>' 31 Width="70"></asp:Label> 32 <%} %> 33 </ItemTemplate> 34 <EditItemTemplate> 35 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("dataMoth") %>'></asp:TextBox> 36 </EditItemTemplate> 37 <ItemStyle HorizontalAlign="Center" /> 38 </asp:TemplateField> 39 <%--4.部门--%> 40 <asp:TemplateField HeaderText="部门" SortExpression="departmentName"> 41 <ItemTemplate> 42 <asp:Label ID="Label2" runat="server" Text='<%# Bind("departmentName") %>' Width="70"></asp:Label> 43 </ItemTemplate> 44 <EditItemTemplate> 45 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("departmentName") %>'></asp:TextBox> 46 </EditItemTemplate> 47 <ItemStyle HorizontalAlign="Center" /> 48 </asp:TemplateField> 49 <%--5.姓名--%> 50 <asp:TemplateField HeaderText="姓名" SortExpression="EName"> 51 <ItemTemplate> 52 <asp:Label ID="Label3" runat="server" Text='<%# Bind("EName") %>' Width="70"></asp:Label> 53 </ItemTemplate> 54 <EditItemTemplate> 55 <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("EName") %>'></asp:TextBox> 56 </EditItemTemplate> 57 <ItemStyle HorizontalAlign="Center" /> 58 </asp:TemplateField> 59 <%--6.基本工资--%> 60 <asp:TemplateField HeaderText="基本工资" SortExpression="basePay"> 61 <ItemTemplate> 62 <asp:TextBox ID="txt_basePay" runat="server" Text='<%# Bind("basePay", "{0:0.00}") %>' 63 Width="70"></asp:TextBox> 64 </ItemTemplate> 65 <EditItemTemplate> 66 <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("basePay", "{0:000.00}") %>'></asp:TextBox> 67 </EditItemTemplate> 68 <ItemStyle HorizontalAlign="Center" /> 69 </asp:TemplateField>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 导入Excel 2 3 protected void btn_Excel_Click(object sender, EventArgs e) 4 { 5 Export("application/ms-excel", "工资表.xls"); 6 } 7 8 public void DisableControls(GridView gv) 9 { 10 for (int i = 0; i < gv.Rows.Count; i++) 11 { 12 TextBox txt = gridView.Rows[i].FindControl("txt_basePay") as TextBox; 13 Label lb = new Label(); 14 lb.Text = txt.Text; 15 gridView.Rows[i].Cells[5].Controls.Add(lb); 16 gridView.Rows[i].Cells[5].Controls.RemoveAt(1); ; 17 } 18 } 19 20 private void Export(string FileType, string FileName) 21 { 22 //GridView导出到Excel,导出所有页 23 Response.Charset = "GB2312"; 24 Response.ContentEncoding = System.Text.Encoding.UTF8;//换成UTF7编码出现乱码 25 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); 26 Response.ContentType = FileType; 27 this.EnableViewState = false; 28 StringWriter tw = new StringWriter(); 29 HtmlTextWriter hw = new HtmlTextWriter(tw); 30 31 this.gridView.AllowPaging = false;//取消分页 32 this.gridView.AllowSorting = false;//取消排序 33 //this.GridView1.AutoGenerateColumns = true;//取消自动生成列 34 if (this.ddl_YesNo_State.SelectedValue == "未发放") 35 { 36 BindData(); 37 } 38 else 39 { 40 BindData_Save(); 41 } 42 DisableControls(gridView); 43 //GridView的独立绑定函数 44 45 //让一列不显示的方法 46 //this.gridView.Columns[0].Visible = false; 47 //this.gridView.Columns[7].Visible = false; 48 //this.gridView.Columns[6].Visible = false; 49 gridView.RenderControl(hw); 50 Response.Write(tw.ToString()); 51 Response.End(); 52 } 53 54 //要导出EXCEL 必须重写VerifyRenderingInServerForm 55 public override void VerifyRenderingInServerForm(Control control) 56 { 57 58 } 59 60 #endregion