Advertisement
panxop

Exportar Gridview a excel a travez de un reportviewer

Aug 30th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Protected Sub btn_Exportar_Click(sender As Object, e As EventArgs) Handles btn_Exportar.Click
  2. Dim warnings As Warning()
  3. Dim streamids As String()
  4. Dim mimeType As String
  5. Dim encoding As String
  6. Dim extension As String
  7. Dim filename As String
  8. 'Consulta que devuelve el dataset con los datos en pantalla
  9. Dim ds As DataSet = clsusr.ExportarUsr(GrillaUsuario, txtUsr_log.Text.Trim(), txtNit_Usr.Text.Trim(), txtNom_Ape.Text.Trim(), txtNit_Emp.Text.Trim(), txtRac_soc.Text.Trim())
  10. Dim ds2 As New DataTable
  11. 'le asigno la tabla 0 del dataset al datatable
  12. ds2 = ds.Tables(0)
  13.  
  14. 'le asigno la fuente al reporte
  15. Dim datos As New ReportDataSource("DataSet1", ds2)
  16. 'le indico donde esta la ruta del reporte a exportar
  17. ReportViewer1.LocalReport.ReportPath = ("Reportes\ReporteMantencionUsrIe.rdlc")
  18. reportViewer1.ProcessingMode = ProcessingMode.Local
  19. ReportViewer1.LocalReport.DataSources.Clear()
  20. 'le agrego el datataset al reporte
  21. reportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", ds.Tables(0)))
  22. reportViewer1.LocalReport.Refresh()
  23.  
  24. 'creo un byte con la extension deseada
  25. Dim bytes As Byte() = ReportViewer1.LocalReport.Render("EXCELOPENXML", Nothing, mimeType, Encoding, extension, streamids, warnings)
  26. 'filename = String.Format("{0}.{1}", "BitacoraWeb_usuario_" & txtusr.Text & "_" & txtdesde.Text.Replace("/", "-") & "_" & txtHasta.Text.Replace("/", "-"), "xlsx")
  27. 'le asigno un nombre al archivo
  28. filename = String.Format("Busqueda_Usuarios_Empresa", "xlsx")
  29.  
  30. Response.ClearHeaders()
  31. Response.Clear()
  32. Response.AddHeader("Content-Disposition", Convert.ToString("attachment;filename=") & filename)
  33. Response.ContentType = mimeType
  34. Response.BinaryWrite(bytes)
  35. Response.Flush()
  36. Response.[End]()
  37. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement