I had been using this code to pass the gridviews to excel but now I want to pass a query: Dim dtM As New DataTable Dim sda As New SqlDataAdapter () SqlStr="SPAparticipantSistance @ TYPE = 7" 'this is the query I want to export to Excel dtM = cnx.CreateDataSource (SqlStr, "connstring")
Dim filename As String = "Modulos_" & DateTime.Now.ToString() & ".xls"
Dim sb As StringBuilder = New StringBuilder()
Dim sw As StringWriter = New StringWriter(sb)
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim pagina As Page = New Page
Dim form = New HtmlForm
'*Convierte linkbuttons y otros objetos a texto
ClearControls(dtgParticipantes)
'dtM.EnableViewState = False
pagina.EnableEventValidation = False
pagina.DesignerInitialize()
pagina.Controls.Add(form)
'form.Controls.Add(dtM)
pagina.RenderControl(htw)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
'Response.ContentType = "application/vnd.word"
Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)
Response.Charset = "UTF-8"
Response.ContentEncoding = Encoding.Default
Response.Write(sb.ToString())
Response.End()