locked
Is it possible on the click of the button to Export To Excel, to take the value entered in the textbox by the user, and display it in the ExcelSpreadsheet as a regular field like the rest of the databound columns? RRS feed

  • Question

  • User2028823583 posted

    Hi. I have a vb.net gridview that gets data from a sql datasource and has a template field.  In that template field, there is  a textbox in an ItemTemplate.     That all works great, the problem I'm having is when the user Exports to Excel.  The gridview looks fine with all the columns, EXCEPT for the column that has that ItemTemplate with the  TextBox.  When it exports to excel, it actually displays a textbox inside the cell of the Excel spreadsheet. 

    Is it possible on the click of the button to Export To Excel, to take the value entered in the textbox by the user, and display it in the ExcelSpreadsheet as a regular field like the rest of the databound columns? 

    Here's my TemplateField from my gridview...

     <asp:TemplateField HeaderText="Salary" SortExpression="Salary">
        <ItemTemplate>
           <asp:TextBox ID="txtSalary" runat="server" Text='<%# Bind("Salary")%>'></asp:TextBox>
       </ItemTemplate>
    </asp:TemplateField>

    and here is my export to Excel

    Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
            Response.Clear()
            Response.Buffer = True
            Response.ContentType = "application/vnd.ms-excel"
            Response.AddHeader("content-disposition", "attachment;filename=" & strFileName)
            Response.Charset = ""
            Me.EnableViewState = False
            Dim StringWriter As New System.IO.StringWriter()
            Dim HtmlTextWriter As New System.Web.UI.HtmlTextWriter(StringWriter)
            gvReport.RenderControl(HtmlTextWriter)
            Response.Write(StringWriter.ToString())
            Response.End()
    End Sub

    Wednesday, March 21, 2018 3:18 PM

Answers

  • User-37275327 posted

    Did you check this one

    https://www.aspsnippets.com/Articles/Export-GridView-with-TemplateField-Column-to-Excel-in-ASPNet.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2018 3:48 PM

All replies

  • User-37275327 posted

    Did you check this one

    https://www.aspsnippets.com/Articles/Export-GridView-with-TemplateField-Column-to-Excel-in-ASPNet.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2018 3:48 PM
  • User2028823583 posted

    Did you check this one

    https://www.aspsnippets.com/Articles/Export-GridView-with-TemplateField-Column-to-Excel-in-ASPNet.aspx

    Thank you!  That works exactly as I need it to.  :-)

    Wednesday, March 21, 2018 4:48 PM
  • User2028823583 posted

      

    Wednesday, March 21, 2018 5:26 PM