Visual Basic.NET - Exportar DDBB de MySQL a Excel

 
Vista:
Imágen de perfil de jose
Val: 42
Ha aumentado su posición en 2 puestos en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Exportar DDBB de MySQL a Excel

Publicado por jose (33 intervenciones) el 12/05/2013 00:57:21
Hola a todos,

Me gustaria saber como exportar toda una base de datos a un fichero excel, he investigado un poco y he conseguido hacer el siguiente codigo mi problema es que dicho codigo primero es eligiendo una referencia de la base de datos el Id, y segundo me da el fallo de:

"InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index"

En cualquiera de estas líneas:


oSheet.Range(a).Value = Listado.Items(i).SubItems.Item(0).Text.ToString
oSheet.Range(b).Value = Listado.Items(i).SubItems.Item(1).Text.ToString
oSheet.Range(c).Value = Listado.Items(i).SubItems.Item(2).Text.ToString
oSheet.Range(d).Value = Listado.Items(i).SubItems.Item(3).Text.ToString
oSheet.Range(j).Value = Listado.Items(i).SubItems.Item(4).Text.ToString

Aqui os dejo el codigo, realmente me interesa saber como exportar toda la base de datos, por lo que creo que este codigo no me valdria.

Private Sub BT_excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_excel.Click


'Variables locales
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Iniciar un nuevo libro en Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

'Agregar datos a las celdas de la primera hoja en el libro nuevo

oSheet = oBook.Worksheets(1)

' Agregamos Los datos que queremos agregar

oSheet.Range("A3").Value = TX_Eliminar.Text()

' Esta celda tendra los datos del textbox

oSheet.Range("A10").Value = "Id"

' estas celdas por defecto solo seran para identificar cada columna


oSheet.Range("B10").Value = "Titulo"

oSheet.Range("C10").Value = "Categoria"

oSheet.Range("D10").Value = "Prestado"

oSheet.Range("E10").Value = "A quien"

'Desde aqui empezaremos a exportar la lista

If Listado.Items.Count > 0 Then

' primero verificamos cuantas filas tiene la lista
Dim col As Integer = 11 'empezaremos en el libro de excel a partir de la celda 11

Dim i As Integer ' for para empezar a recorrer la lista

For i = 0 To Listado.Items.Count
If i = Listado.Items.Count Then

Exit For
End If
' estas son las columnas que usaremos y el contador nos ira cargando una a una cada fila
Dim a As String = "A" + col.ToString + ""
Dim b As String = "B" + col.ToString + ""
Dim c As String = "C" + col.ToString + ""
Dim d As String = "D" + col.ToString + ""
Dim j As String = "E" + col.ToString + ""

oSheet.Range(a).Value = Listado.Items(i).SubItems.Item(0).Text.ToString
oSheet.Range(b).Value = Listado.Items(i).SubItems.Item(1).Text.ToString
oSheet.Range(c).Value = Listado.Items(i).SubItems.Item(2).Text.ToString
oSheet.Range(d).Value = Listado.Items(i).SubItems.Item(3).Text.ToString
oSheet.Range(j).Value = Listado.Items(i).SubItems.Item(4).Text.ToString

col = col + 1

'Listado.Items.Remove(Listado.Items.Item(i))

' removemos cada el elemento esto es solo si desean hacerlo
' otra opcion es poner el contador positivo y no eliminar el elemento de la lista

i = i - 1
Next


End If
' hacemos visible el documento
oExcel.Visible = True
oExcel.UserControl = True
'Guardaremos el documento en el escritorio con el nombre prueba
oBook.SaveAs(Environ("UserProfile") & "\Desktop\Prueba.xls")
End Sub
End Class
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder