Foros del Web » Programación para mayores de 30 ;) » .NET »

Automatizar v.b 2010 con excel

Estas en el tema de Automatizar v.b 2010 con excel en el foro de .NET en Foros del Web. Saludos amigos del foro, deseo hacer un reporte excel desde el visual 2010. se agradece cualquier ayuda....
  #1 (permalink)  
Antiguo 17/11/2010, 20:07
Avatar de frankjoel86  
Fecha de Ingreso: noviembre-2008
Ubicación: Ica
Mensajes: 295
Antigüedad: 15 años, 5 meses
Puntos: 0
Automatizar v.b 2010 con excel

Saludos amigos del foro, deseo hacer un reporte excel desde el visual 2010. se agradece cualquier ayuda.
__________________
www.basritours.com
Agencia de turismo en Ica.
  #2 (permalink)  
Antiguo 17/11/2010, 21:50
Avatar de Aquaventus  
Fecha de Ingreso: junio-2010
Ubicación: Lima-Peru , En el alba de la naturaleza
Mensajes: 2.105
Antigüedad: 13 años, 10 meses
Puntos: 267
Respuesta: Automatizar v.b 2010 con excel

Hola, en internet hay muchos ejemplos de como hacer reporte en vb.net, pero bueno colaborandote te dejo 1:

Primero creamos la conexión a la base de datos SQL Server 2000• Creamos una Clase = Conexion.vb
Código:

Imports System.Data.SqlClient

Public Class conexion
'Variables
Public build As New SqlConnectionStringBuilder
Public cnn As New SqlConnection(build.ConnectionString)

Public Sub conexion()
build = New SqlConnectionStringBuilder
With build
.DataSource = "(Local)"
.InitialCatalog = "Northwind"
.IntegratedSecurity = True
End With
cnn = New SqlConnection(build.ConnectionString)
Try
cnn.Open()
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
liberarTodo()
Exit Sub
End Try
End Sub

Public Sub liberarTodo()
cnn.Close()
End Sub
End Class
________________________________________
Segundo En el WebForm Ponemos el siguiente codigo
Código:

Imports Microsoft.Office.Interop
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page
Dim vconexion As New Conexion

Dim m_Excel As Excel.Application
Dim CategoryName As String 'Variable para controlar la ruptura por nombre de categorías
Dim objLibroExcel As Excel.Workbook 'Creamos un objeto WorkBook
Dim objHojaExcel As Excel.Worksheet 'Creamos un objeto WorkSheet
Dim objDataSet As New Data.DataSet("ExcelTest")
Dim i As Integer = 5

Protected Sub BtnCrear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCrear.Click
vconexion.conexion()

Dim objSQLAdapter As New SqlDataAdapter("SELECT CategoryName,ProductID,ProductName,UnitPrice FROM Products,Categories WHERE Products.CategoryID = Categories.CategoryID ORDER BY Categories.CategoryID", vconexion.cnn)
Dim objDataSet As New Data.DataSet("ExcelTest")

objSQLAdapter.Fill(objDataSet, "Categories")

m_Excel = New Excel.Application
m_Excel.Visible = True

objLibroExcel = m_Excel.Workbooks.Add()
objHojaExcel = objLibroExcel.Worksheets(1)
objHojaExcel.Visible = Excel.XlSheetVisibility.xlSheetVisible

objHojaExcel.Activate()

objHojaExcel.Range("A1:G1").Merge()
objHojaExcel.Range("A1:G1").Value = "Ejemplo de Como Hacer Reportes de Excel en VB.NET"
objHojaExcel.Range("A1:G1").Font.Bold = True
objHojaExcel.Range("A1:G1").Font.Size = 15

objHojaExcel.Range("A2:D2").Merge()
objHojaExcel.Range("A2:D2").Value = "By:alex19910218"
objHojaExcel.Range("A2:D2").Font.Italic = True
objHojaExcel.Range("A2:D2").Font.Size = 13

objHojaExcel.Range("A4").Value = "Categoría"
objHojaExcel.Range("A4").Font.Bold = True
objHojaExcel.Range("A4").Font.Size = 12

objHojaExcel.Range("B4").Value = "Código"
objHojaExcel.Range("B4").Font.Bold = True
objHojaExcel.Range("B4").Font.Size = 12

objHojaExcel.Range("C4").Value = "Nombre"
objHojaExcel.Range("C4").Font.Bold = True
objHojaExcel.Range("C4").Font.Size = 12

objHojaExcel.Range("D4").Value = "Precio RD$"
objHojaExcel.Range("D4").Font.Bold = True
objHojaExcel.Range("D4").Font.Size = 12

CategoryName = ""

For Each objRow As Data.DataRow In objDataSet.Tables(0).Rows
CategoryName = objRow.Item(0)

objHojaExcel.Cells(i, "A") = objRow.Item(0) 'CategoryName'
objHojaExcel.Cells(i, "B") = objRow.Item(1) 'ProductID'
objHojaExcel.Cells(i, "C") = objRow.Item(2) 'ProductName'
objHojaExcel.Cells(i, "D") = objRow.Item(3) 'UnitPrice'

i += 1
Next
End Sub
End Class

Etiquetas: automatizar, excel
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 21:42.