RE:EXPORTAR A EXCEL DE UN LISTVIEW
Esta es una función que estoy utilizando para exportar desde un ListView a Excel
Recuerden hacer referencia en el proyecto a Microsoft Excel x.0 (x.0 será la versión que tengan instalada...)
Quiero aclarar que llegué a esto desde distintas ayudas de otras almas caritativas que aportan sus conocimientos en la red, y creo que una forma de ser ser agradecido es compartiéndolo con quien lo necesite.
A todos ellos muchas gracias.
Function Exportar_Excel(ByVal Path_Libro As String, ByVal List As ListView, _
Optional Progressbar As Progressbar) As Boolean
'CREAR EL OBJETO (INSTANCIAR)CON EL OBJETO APLICACION (obj_Excel)
Dim obj_Excel As Object
Dim obj_Libro As Object
' Nueva referencia a Excel y nuevo referencia al Libro
Set obj_Excel = CreateObject("Excel.Application")
Set obj_Libro = obj_Excel.Workbooks.Add '(Path_Libro)
'Variables para las columnas y filas
Dim Col As Integer, Fila As Integer
'Variables para determinar rangos
Dim desde As String, hasta
With obj_Libro
'Asignamos El valor Maximo del Progress teniendo _
como dato la cantidad de items en el ListView
If Not Progressbar Is Nothing Then
Progressbar.Max = List.ListItems.Count '+ 2
End If
'Referencia a la hoja con índice 1
With .Sheets(1)
'Incorporamos las columnas del ListView
For k = 1 To List.ColumnHeaders.Count
.Cells(1, k) = List.ColumnHeaders(k).Text
Next k
'Recorremos la cantidad de items del ListView
For Fila = 1 To List.ListItems.Count
Col = 1
'Asignamos EL Item actual en la celda
.Cells(Fila + 1, Col) = List.ListItems.item(Fila)
'Asignamos EL SubitemItem actual en la celda
For Col = 1 To List.ColumnHeaders.Count - 1
'este Select es para formatear las celdas al mismo tiempo de asignarles
'el valor del SubitemItem
Select Case Col
'Aquí tengo fecha
Case 2
'.Cells(Fila + 1, Col + 1).numberformat = "dd/mm/yy"
.Cells(Fila + 1, Col + 1) = List.ListItems(Fila).SubItems(Col)
'Aqui tengo importes
Case 4, 5, 6
'.Cells(Fila + 1, Col + 1).numberformat = "#,#0.00"
.Cells(Fila + 1, Col + 1) = Str$(List.ListItems(Fila).SubItems(Col))
'Aquí no hace falta formatear
Case Else
.Cells(Fila + 1, Col + 1) = List.ListItems(Fila).SubItems(Col)
End Select
Next
If Not Progressbar Is Nothing Then
'Aumentamos en 1 la propiedad value
Progressbar.value = Progressbar.value + 1
End If
Next
'Introducimos la funcion Suma para las columnas de los importes
desde = "E2"
hasta = "E" & List.ListItems.Count + 1
rango = desde & ":" & hasta
.Cells(List.ListItems.Count + 2, "E").Formula = "=SUM(" & rango & ")" 'E2:E97)"
desde = "F2"
hasta = "F" & List.ListItems.Count + 1
rango = desde & ":" & hasta
.Cells(List.ListItems.Count + 2, "F").Formula = "=SUM(" & rango & ")"
desde = "G2"
hasta = "G" & List.ListItems.Count + 1
rango = desde & ":" & hasta
.Cells(List.ListItems.Count + 2, "G").Formula = "=SUM(" & rango & ")"
'Esto es para alinear los importes a la derecha que estan en las columnas 4,5 y 6
desde = "E2"
hasta = "G" & List.ListItems.Count + 2
.Cells.Range(desde, hasta).HorizontalAlignment = xlRight
.Cells.Range(desde, hasta).numberformat = "#,#0.00"
'lo mismo para la columna 2 que estan las fechas
desde = "C2"
hasta = "C" & List.ListItems.Count
.Cells.Range(desde, hasta).HorizontalAlignment = xlRight
.Cells.Range(desde, hasta).numberformat = "dd/mm/yy"
'Formateamos los encabezados
'...alineación centrada
desde = "A1"
hasta = "G1"
.Cells.Range(desde, hasta).HorizontalAlignment = xlCenter
'...negrita
.Cells.Range(desde, hasta).Font.Bold = True
'...borde
.Cells.Range(desde, hasta).Borders.LineStyle = xlDouble
'Borde para el resto de las celdas
desde = "A2"
hasta = "G" & List.ListItems.Count + 1
With .Cells.Range(desde, hasta).Borders
.LineStyle = 1
.Weight = 2
.ColorIndex = xlAutomatic
End With
'.WorksheetFunction.Sum (.Cells.Range(desde, hasta))
'Ajuste automático del ancho de las columnas
.Columns.EntireColumn.AutoFit
End With
End With
'Destribuimos las variables de objeto
obj_Excel.activeworkbook.saveas Path_Libro
obj_Excel.activeworkbook.Close
'obj_Excel.Visible = True
Set obj_Libro = Nothing
Set obj_Excel = Nothing
'Ok
Exportar_Excel = True
If Not Progressbar Is Nothing Then
Progressbar.value = 0.0001
End If
Exit Function
errSub:
Exportar_Excel = False
MsgBox Err.Description, vbCritical
On Error Resume Next
Set obj_Libro = Nothing
Set obj_Excel = Nothing
Progressbar.value = 0
End Function