locked
<th> hide in webpage but visible in excel export RRS feed

  • Question

  • User-294002540 posted

    I hide <th> tag with code behind but it visible when I export to excel

    My code:

    VB

    Sub showhide()
    If CBsales.Checked = True Then
    
    
                ClientScript.RegisterStartupScript(Me.[GetType](), "C0001", "document.getElementById('ta011').style.display = 'table-cell';", True)
       Dim i As Integer = 0
                For i = 1 To 10 Step 1
                    GridView2.Columns(i).Visible = True
                Next
    
            ElseIf CBsales.Checked = False Then
    
                ClientScript.RegisterStartupScript(Me.[GetType](), "C0007", "document.getElementById('ta011').style.display = 'none';", True)
    
       Dim i As Integer = 0
                For i = 1 To 10 Step 1
                    GridView2.Columns(i).Visible = True
                Next
    end sub
    
     Protected Sub BTexport_click(sender As Object, e As EventArgs) Handles BTexport.Click
            Try
    
                Response.Clear()
                Response.ClearContent()
                Response.Buffer = True
                Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "TestPage.xls"))
                Response.ContentEncoding = Encoding.UTF8
                Response.ContentType = "application/ms-excel"
                Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
                Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
    
                export.RenderControl(hw)
    
                Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
                Response.Write(style)
                Response.Write(sw.ToString())
                Response.Flush()
                Response.[End]()
            Catch ex As Exception
            End Try
        End Sub
     <div id ="export" runat="server">
     <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowFooter="true" FooterStyle-BackColor="#add8e6" FooterStyle-Height="30px" >
                     
                           <Columns>
                               
                                <asp:TemplateField HeaderStyle-BorderStyle="None">
                                    <HeaderTemplate>
                                       
                                          <tr class="fonthead">
                                            <th id="tb011" colspan="2" rowspan="2" style="height:80px; background-color: #d9d9d9;  ">
                                                test01
                                            </th>
    </tr>
     </HeaderTemplate>
                                       
                                </asp:TemplateField>
    
    <asp:BoundField DataField="ProductID"  HeaderText="test01" HtmlEncode="false" ShowHeader="false" ItemStyle-Height="30"
                                HeaderStyle-Height="34px" ItemStyle-Width="110px" HeaderStyle-BackColor="#d9d9d9" />
    </columns>
     </asp:GridView>
                            
                 </div>


    Thursday, February 6, 2020 9:58 AM

All replies

  • User409696431 posted

    The CSS attribute display:none still renders the code in markup for the page (view source in your browser and you'll see it), just with a style that won't display it.

    On the other hand, a control set to ASP.NET's visible=false isn't rendered in the markup of the page at all.  Unfortunately, the "th" isn't a control. not even an HTML control since it has no runat="server".

    You could try instead, in your javascript, to remove the element, not change its style. Something along the lines of

    var element = document.getElementById('ta011'); element.parentNode.removeChild(element);

    Thursday, February 6, 2020 12:12 PM
  • User-719153870 posted

    Hi Tomato Cultivator,

    You want to hide your Gridview header when the Gridview exported to Excel?

    If so, set the header visible to false then the table in Excel will contain no header.

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <div id="export" runat="server">
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" SelectCommand="select * from Users"></asp:SqlDataSource>
                    <asp:GridView ID="GridView2" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" ShowFooter="true" FooterStyle-BackColor="#add8e6" FooterStyle-Height="30px">
    
                        <Columns>
    
                            <asp:TemplateField HeaderStyle-BorderStyle="None">
                                <HeaderTemplate>
    
                                    <tr class="fonthead">
                                        <th id="tb011" colspan="2" rowspan="2" style="height: 80px; background-color: #d9d9d9;">test01
                                        </th>
                                    </tr>
                                </HeaderTemplate>
    
                            </asp:TemplateField>
                            <asp:BoundField DataField="UID" HeaderText="test01" HtmlEncode="false" ShowHeader="false" ItemStyle-Height="30"
                                HeaderStyle-Height="34px" ItemStyle-Width="110px" HeaderStyle-BackColor="#d9d9d9" />
                            <asp:BoundField DataField="UName" HeaderText="test01" HtmlEncode="false" ShowHeader="false" ItemStyle-Height="30"
                                HeaderStyle-Height="34px" ItemStyle-Width="110px" HeaderStyle-BackColor="#d9d9d9" />
                        </Columns>
                    </asp:GridView>
                    <asp:Button ID="BTexport" runat="server" Text="BTexport" />
                </div>
            </div>
        </form>
    </body>
    </html>

    vb:

    Protected Sub BTexport_click(sender As Object, e As EventArgs) Handles BTexport.Click
            Try
                ExportExcel("test", GridView2)
            Catch ex As Exception
            End Try
        End Sub
    
        Public Sub ExportExcel(ByVal filename As String, ByVal gv As GridView)
            Response.ClearContent()
            Response.AddHeader("content-disposition", "attachment; filename=" & filename & ".xls")
            Response.ContentType = "application/vnd.ms-excel"
            gv.HeaderRow.Visible = False
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            gv.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.[End]()
        End Sub

    Best Regard,

    Yang Shen

    Friday, February 7, 2020 6:31 AM