i have export excle file with the help of below code, but in my web page gridview genrated every time when loop executed.how i set visual false. please help mee.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"></asp:GridView>
VB.net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Function BindGrid() As DataTable
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt1.Rows.Add(1, "John Hammond", "United States")
dt1.Rows.Add(2, "Mudassar Khan", "India")
dt1.Rows.Add(3, "Suzanne Mathews", "France")
dt1.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt1
GridView1.DataBind()
ViewState("dt1") = dt1
Return dt1
End Function
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim dt1 As DataTable = TryCast(ViewState("dt1"), DataTable)
Dim j As Integer = 0
While j < dt1.Rows.Count
ExportToExcel()
GridView1.Visible = False
j = j + 1
End While
End Sub
Private count As Integer = 0
Public Sub ExportToExcel()
Dim a As String = "Book1"
Dim path As String = Server.MapPath("~/File")
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
If File.Exists(path & "/File" & a & "_" & Convert.ToString(count) & ".xls") Then
File.Delete(path & "/File" & a & "_" & Convert.ToString(count) & ".xls")
End If
Dim writer As StreamWriter = File.CreateText(path & "/File" & a & "_" & Convert.ToString(count) & ".xls")
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
Me.BindGrid()
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
writer.Write(sw.ToString())
writer.Flush()
writer.Close()
writer.Dispose()
Response.Flush()
End Using
End Using
count += 1
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub