Senorita

Senorita

  • 1.6k
  • 123
  • 8.7k

Export .xlsx from Gridview in vb.net using asp.net

Nov 14 2019 10:18 PM
hi i am trying to export gridview data in xlsx file using below code, but when i open file its showing error, 
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx")
Response.Charset = ""
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
For Each r As GridViewRow In GridView1.Rows
If r.RowType = DataControlRowType.DataRow Then
For columnIndex As Integer = 0 To r.Cells.Count - 1
r.Cells(columnIndex).Attributes.Add("class", "text")
Next
End If
Next
GridView1.RenderControl(htmlWrite)
Dim style As String = "&lt;style> .text { mso-number-format:\@; } </style> "
Response.Write(style)
Response.Write(stringWrite.ToString())
Response.[End]()

Answers (7)