Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/03/2007, 15:11
juantiva
 
Fecha de Ingreso: junio-2006
Mensajes: 109
Antigüedad: 18 años, 6 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