CARRERA DE COMPUTACIÓN E INFORMÁTICA
Codificación en la Clase cls_Conecta.
Dim cnn As New SqlConnection("server=(local);uid=sa;pwd=;database=northwind")
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Public Function buscar() As DataTable
da.SelectCommand = New SqlCommand("select productid,productname from products Where
productname like 'C%'", cnn)
da.Fill(ds, "Products")
Return ds.Tables(0)
End Function
' Agregar Referencia a Microsoft Excel 11.0 en COM
Imports Microsoft.Office.Interop
Imports bcAccesodatos
Declaración de Objetos en el Formulario.
Dim m_Excel As New Excel.Application
Dim objconecta As New clsConecta
Evento LOAD del Formulario.
grdMostrar.DataSource = objconecta.buscar()
Evento Click del Botón Exportar.
Dim objLibroExcel As Excel.Workbook = m_Excel.Workbooks.Add()
Dim objHojaExcel As Excel.Worksheet = objLibroExcel.Worksheets(1)
m_Excel.Visible = True
objHojaExcel.Range("A3:A3").Value = "Código"
objHojaExcel.Range("A3:A3").Font.Size = 12
objHojaExcel.Range("B3:B3").Value = "Producto"
objHojaExcel.Range("B3:B3").Font.Size = 12
Dim i As Integer = 4
For Each objRow As DataRow In objconecta.buscar().Rows
objHojaExcel.Cells(i, "A") = objRow.Item(0)
objHojaExcel.Cells(i, "B") = objRow.Item(1)
i=i+1
Next
btnExportar.Enabled = False
Evento Closing del Formulario.
If Not m_Excel Is Nothing Then
m_Excel.Quit()
m_Excel = Nothing
End If
CURSO: SEMINARIO DE SISTEMAS I Semana 6 Página
DOCENTE: LUIS VALVERDE LÓPEZ
[email protected] 2007 - 1