Senorita

Senorita

  • NA
  • 123
  • 8.7k

export excel file using gridview in asp.net using vb.net

Nov 22 2018 7:34 AM
Hi i am creating excle file inside the for loop :- using below code
 
GridView1.DataBind()
GridView1.DataSource = dt1
GridView1.DataBind()
Dim path As String = Server.MapPath("~/" & (ConfigurationSettings.AppSettings("DocketAnnexure")))
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
If File.Exists(path & "/Invoice_Annexure_details" & awbno + "_" + Convert.ToString(count) & ".xls") Then
File.Delete(path & "/Invoice_Annexure_details" & awbno + "_" + Convert.ToString(count) & ".xls")
End If
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
Dim writer As StreamWriter = File.CreateText(path & "/Invoice_Annexure_details" & awbno + "_" + Convert.ToString(count) & ".xls")
Try
GridView1.RenderControl(hw)
writer.WriteLine(sw.ToString())
writer.Flush()
writer.Close()
''hw.Flush()
''hw.Close()
''hw.Dispose()
writer.Dispose()
sw.Flush()
sw.Close()
sw.Dispose()
GC.Collect()
Catch ex As Exception
writer.Flush()
writer.Close()
'hw.Flush()
'hw.Close()
'hw.Dispose()
writer.Dispose()
sw.Flush()
sw.Close()
sw.Dispose()
GC.Collect()
End Try
End Using
End Using
 
my first file as been created with data but when i create next file its blank creat 

Answers (1)