Foros del Web » Programando para Internet » ASPX (.net) »

exportar listado a excel

Estas en el tema de exportar listado a excel en el foro de ASPX (.net) en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 15/03/2007, 02:44
 
Fecha de Ingreso: diciembre-2004
Mensajes: 32
Antigüedad: 19 años, 4 meses
Puntos: 0
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!
  #2 (permalink)  
Antiguo 16/03/2007, 15:11
 
Fecha de Ingreso: junio-2006
Mensajes: 109
Antigüedad: 17 años, 10 meses
Puntos: 2
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
  #3 (permalink)  
Antiguo 16/03/2007, 15:41
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 19 años, 4 meses
Puntos: 7
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:
Try
    ' ...
Catch e As Exception
    Throw e
End Try
Generalmente es mala idea capturar excepciones generales y más aún no realizar ningún proceso con éstas.

Te recomiendo una lectura muy interesante al respecto:
http://blogs.msdn.com/fxcop/archive/...era_5D00_.aspx

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
  #4 (permalink)  
Antiguo 16/03/2007, 15:54
 
Fecha de Ingreso: junio-2006
Mensajes: 109
Antigüedad: 17 años, 10 meses
Puntos: 2
Re: exportar listado a excel

Te agradezco el comentario pero era para ejemplicar solamente la parte del excel

Suerte
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:34.