0
Answer

Problem in exporting grid view to excel file

Geetha Mouli

Geetha Mouli

14y
3.1k
1

Hi,
I have export to excel option for all my reports. In few of report, if i export to excel file the file just has 'No records found' the text which i have mensioned in <EmptyDataTemplate> In few other reports i am not getting the proper value or proper format. I don't have any clue why it is happening like that.

Here is my code.

.aspx file


<
asp:GridView ID="GridView1" runat="server" CssClass="txt"
AlternatingRowStyle-BorderStyle="Ridge" BorderStyle="Ridge" CellSpacing="2"
BorderColor="#333333" CellPadding="2">
<EmptyDataTemplate>
No records found.
</EmptyDataTemplate>
</asp:GridView>

.vb page

On page load...

Dim
strQuery As String = "SELECT Gate, COUNT(TicketInfo) AS 'People Entered' FROM tbl_access_info"
Dim cmd As SqlCommand = New SqlCommand(strQuery, New SqlConnection(strConnectionString))

cmd.Connection.Open()
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()


Private
Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
Response.Clear()
Response.Buffer =
True
Response.ContentType =
"application/vnd.ms-excel"
Response.Charset =
""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
GridView1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.[End]()
End Sub

Can u help me to identify the problem? Thanks in advance.

Geetha