这是我的GridView:
<asp:GridView ID="GridView1" runat="server" BorderColor="#ececec" BorderWidth="2px" HeaderStyle-BackColor="#F6F6F6" HeaderStyle-Font-Bold="true" RowStyle-BackColor="white" AlternatingRowStyle-BackColor="#F6F6F6" AutoGenerateColumns="False" ShowFooter="true" DataKeyNames="UsuarioId" ShowHeaderWhenEmpty="True" OnRowCommand="GridView1_OnRowCommand" OnRowEditing="GridView1_OnRowEditing" OnRowCancelingEdit="GridView1_OnRowCancelingEdit" OnRowUpdating="GridView1_OnRowUpdating" OnRowDeleting="GridView1_OnRowDeleting" Width="100%" CssClass="GridViewStyle" >
<EmptyDataTemplate>
<div>
No hay datos para mostrar :(
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Tipo Usuario ID (Dato momentaneo)">
<ItemTemplate>
<asp:Label Text='<%#Eval("tipo") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTipoUsuarioId" Text='<%#Eval("tipo") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropdown" runat="server" AppendDataBoundItems="True" DataTextField="tipoUsuario" DataValueField="tipoUsuarioId" DataSourceID="sqlDB" CssClass="ddl">
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDB" ConnectionString="<%$ ConnectionStrings:conn%>" runat="server" SelectCommand="Select * from Tipo_Usuario"></asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nombre">
<ItemTemplate>
<asp:Label Text='<%#Eval("nombre") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNombre" Text='<%#Eval("nombre") %>' runat="server" required="true"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNombreFooter" runat="server"></asp:TextBox>
<br/>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" ForeColor="Red" runat="server" ValidationGroup="Group1" ControlToValidate="txtNombreFooter"
ErrorMessage="Requiere nombre!"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contraseña">
<ItemTemplate>
<asp:Label Text='<%#Eval("contraseña") %>' runat="server" >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContraseña" Text='<%#Eval("contraseña") %>' runat="server" required="true" ></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtContraseñaFooter" runat="server" ></asp:TextBox>
<br/>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" ForeColor="Red" runat="server" ValidationGroup="Group1" ControlToValidate="txtContraseñaFooter"
ErrorMessage="Requiere contraseña!"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Habilitado">
<ItemTemplate>
<asp:Label id="habilitado" Text='<%#Eval("habilitado") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHabilitado" Text='<%#Eval("habilitado") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CssClass="btn btn-warning" BackColor="#EC971F" Text="Editar" ForeColor="white" CommandName="Edit" runat="server" ><i class="fa fa-pencil" aria-hidden="true"></i></asp:LinkButton>
<asp:LinkButton CssClass="btn btn-danger" Text="Borrar" CommandName="Delete" runat="server" > <i class="fa fa-trash-o" aria-hidden="true"></i></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="Assets/ICONS/Save.png" CommandName="Update" ToolTip="Update" Width="20" Height="20" runat="server"/>
<asp:ImageButton ImageUrl="Assets/ICONS/Cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20" Height="20" runat="server"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="Assets/ICONS/xls.png" OnClick="ExportDataToExcel" Width="40" Height="40" runat="server"/>
<asp:LinkButton CssClass="btn btn-success" Text="Agregar" CommandName="Agregar" runat="server" ValidationGroup="Group1"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是要导出的代码:
public void ExportDataToExcel(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.AutoGenerateEditButton = false;
GridView1.AutoGenerateDeleteButton = false;
Response.ClearContent(); //Clear any content
Response.AppendHeader("content-disposition","attachment; filename=Usuarios.xls"); //Export as XLS (Excel)
Response.ContentType = "application/excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); //Saves all the content including rows.
GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString()); //Gridview data is going to be converted for a xls file
Response.End();
}
这就是我得到的:
就像我在标题上说的那样,我想在导出到excel时隐藏TextBox和按钮,以便只查看数据。这些控件位于EditItemTemplate
标记内。在aspx网站上,GridView显示来自SQL Server数据库的数据,我想要做的是将其导出为表格。