I am new to .net. I have written a code to export a gridview into excel. the export works but instead of getting data of gridview I get tags in the exported document. Same case if I exported the gridview to docs. This is what exported file looks like
The GridView I have gets populated by a search query. Here is an excerpt of my code:
Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
MsgBox("Exporting")
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
gridviewID.AllowPaging = False
gridviewID.DataSource = gvDataTable
gridviewID.DataBind()
gridviewID.RenderControl(hw)
Response.Write(sw.ToString())
Response.Flush()
Response.End()
My GridView is not inside a Form tag as I already have a Form tag in my master page. This is how my GridView looks:
<asp:GridView ID="gridviewID" runat="server"
AutoGenerateColumns="False" onrowcommand="searchResultsGridView_RowCommand">
<Columns>
<asp:BoundField DataField="logNum" HeaderText="ID" ReadOnly="True" ItemStyle-CssClass="hideme" />
<asp:BoundField DataField="workIDName" HeaderText="work NAME"
SortExpression="workIDName" ReadOnly="True" />
<asp:BoundField DataField="LeadIdName" HeaderText=" LEAD"
SortExpression="LeadIdName" ReadOnly="True" />
<asp:BoundField DataField="Lead_Name2"// and a lot of fields till closing tags
I have placed verifyrendering function right after my page load. Placing this function right after export function wouldn't work. It would not let me export at all.
What could i be possibly doing wrong?