Buenos días, alguien sabe cómo exportar un listado a excel desde ASPX?
Tendré un botón bajo el listado y al pulsarlo debería decir si lo quieres guardar, abrir , cancelar..
Muchas gracias de antemano y saludos a todos!
| |||
| exportar listado a excel Buenos días, alguien sabe cómo exportar un listado a excel desde ASPX? Tendré un botón bajo el listado y al pulsarlo debería decir si lo quieres guardar, abrir , cancelar.. Muchas gracias de antemano y saludos a todos! |
| |||
| Re: exportar listado a excel Te paso un ejemplo, tienes que ocultar todos los botones, y demas controles que no quieres que pase al excel. Suerte
Código:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Excel
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents btnexcel As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.Page.IsPostBack Then
CargaGrid()
End If
End Sub
Private Sub CargaGrid()
Me.DataGrid1.DataSource = Run()
Me.DataGrid1.DataBind()
End Sub
Private Function Run() As DataSet
Dim myConnection As SqlConnection
Dim mySqlDataAdapter As SqlDataAdapter
Try
Dim myDataSet As DataSet = New DataSet
myConnection = New SqlConnection("Server=.; UID=sa; PWD=sa; Initial Catalog=northwind; Connect Timeout=15000")
mySqlDataAdapter = New SqlDataAdapter("select top 10 * from customers", myConnection)
mySqlDataAdapter.Fill(myDataSet, "Customers")
Return myDataSet
Catch e As Exception
Throw e
End Try
End Function
Private Sub btnexcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexcel.Click
Try
With Me.DataGrid1
.CurrentPageIndex = 0
.AllowSorting = False
.AllowPaging = False
.EnableViewState = False
End With
CargaGrid()
'////////////////////////
'Tienes que ocultar todos los controles que no quieras mostrar en el excel
Me.btnexcel.Visible = False
'////////////////////////
Me.EnableViewState = False
Response.ClearHeaders()
Response.Charset = "ISO-8859-1"
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", ("attachment;filename=NombreDelArchivo_" & Format(Now(), "yyyyMMdd") & ".xls"))
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
|
| ||||
| Re: exportar listado a excel Aunque mi comentario está fuera de tema, pero me llama la atención la siguiente porción de código:
Código:
Generalmente es mala idea capturar excepciones generales y más aún no realizar ningún proceso con éstas.Try
' ...
Catch e As Exception
Throw e
End Try
Te recomiendo una lectura muy interesante al respecto: http://blogs.msdn.com/fxcop/archive/...era_5D00_.aspx Saludos |