Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Exportar A Excel Vb6
Exportar A Excel Vb6
Excel
<Volver> - Anterior - Siguiente
Hay una función llamada Exportar_Excel a la cual se le debe pasar el path y el nombre del
Libro. El segundo parámetro es el control ListView, el tercer parámetro es opcional y es
para el Progressbar, el último parámetro es el índice de la hoja. Por ejemplo :
Ejemplo
Texto planoImprimir
1. Option Explicit
2.
3.
4. ' ------------------------------------------------------------------
5. ' \\ -- Función para exportar los datos
6. ' ------------------------------------------------------------------
7. Function Exportar_Excel( _
8. sFileName As String, _
9. ListView As ListView, _
10. Optional Progressbar As Progressbar, _
11. Optional SheetIndex As Integer = 1) As Boolean
12.
13. On Error GoTo error_Handler
14.
15. Dim obj_Excel As Object ' --
CREAR EL OBJETO (INSTANCIAR)CON EL OBJETO APLICACION (obj_Exc
el)
16. Dim obj_Libro As Object
17.
18. Dim iCol As Integer ' -- Variables para las columnas y filas
19. Dim iRow As Integer
20.
21. ' -- Nueva referencia a Excel y nuevo referencia al Libro
22. Set obj_Excel = CreateObject("Excel.Application")
23. With obj_Excel
24. ' -- Abrir el libro
25. Set obj_Libro = .Workbooks.Open(sFileName)
26. End With
27.
28. With obj_Libro
29.
30. ' -- Asignamos El valor Maximo del Progress teniendo
31. ' -- como dato la cantidad de items en el ListView
32. If Not Progressbar Is Nothing Then
33. Progressbar.Max = ListView.ListItems.Count
34. If Not Progressbar.Visible Then Progressbar.Visible = True
35. End If
36.
37. ' -- Referencia a la hoja con índice 1
38. With .Sheets(SheetIndex)
39. ' -- Recorremos la cantidad de items del ListView
40. For iRow = 1 To ListView.ListItems.Count
41. iCol = 1
42. ' -- Asignamos el item actual en la celda
43. .Cells(iRow, iCol) = ListView.ListItems.Item(iRow)
44.
45. ' -- Asignamos el subitem actual en la celda
46. For iCol = 1 To ListView.ColumnHeaders.Count - 1
47. .Cells(iRow, iCol + 1) = ListView.ListItems(iRow).SubItems(iCol)
48. Next
49.
50. If Not Progressbar Is Nothing Then
51. ' -- Aumentamos en 1 la propiedad value
52. Progressbar.Value = Progressbar.Value + 1
53. End If
54. Next
55. End With
56. End With
57.
58. ' -- Destruimos las variables de objeto
59. obj_Excel.Visible = True
60. Set obj_Libro = Nothing
61. Set obj_Excel = Nothing
62. ' -- Ok
63. Exportar_Excel = True
64.
65. If Not Progressbar Is Nothing Then
66. Progressbar.Value = 0
67. Progressbar.Visible = False
68. End If
69.
70.
71. ' -- Errores
72. Exit Function
73. error_Handler:
74.
75. Exportar_Excel = False
76. MsgBox Err.Description, vbCritical
77.
78. On Error Resume Next
79. Set obj_Libro = Nothing
80. Set obj_Excel = Nothing
81.
82. Progressbar.Value = 0
83. End Function
84.
85.
86. ' ------------------------------------------------------------------
87. ' \\ -- Botón para exportar los datos al libro
88. ' ------------------------------------------------------------------
89. Private Sub Command1_Click()
90.
91. Dim ret As Boolean
92.
93. ' -- Le pasa el path donde está ubicado el libro, _
94. -- el control ListView, opcional un Progressbar, _
95. -- y lo exporta en la hoja con índice 2
96. ret = Exportar_Excel("c:\libro1.xls", ListView1, ProgressBar1, 2)
97.
98. If ret Then
99. ' -- OK
100. MsgBox " Datos exportados a Excel OK ", vbInformation
101. End If
102.
103. End Sub
104. ' ------------------------------------------------------------------
105. ' \\ -- Inicio
106. ' ------------------------------------------------------------------
107.
108. Private Sub Form_Load()
109.
110. Dim lvItem As ListItem
111. Dim i As Integer
112.
113. ' -- Configurar ListView
114. With ListView1
115. .ColumnHeaders.Add , , " Columna 1 "
116. .ColumnHeaders.Add , , " Columna 2 "
117. .View = lvwReport
118. End With
119.
120. ' -- Añadir algunos items al listview
121. For i = 0 To 5000
122. Set lvItem = ListView1.ListItems.Add(, , " Item " & i)
123. lvItem.SubItems(1) = "subitem " & i
124. Next
125.
126. Command1.Caption = " > Exportar Listview a Excel "
127.
128. End Sub
129. ' ------------------------------------------------------------------
130. ' \\ -- Redimensionar controles
131. ' ------------------------------------------------------------------
132. Private Sub Form_Resize()
133. With ListView1
134. .Move 0, 0, Me.ScaleWidth, Me.ScaleHeight -
(Command1.Height + 50)
135. Command1.Move Me.ScaleWidth -
(Command1.Width + 50), .Top + .Height + 50
136. End With
137. With Command1
138. ProgressBar1.Move 0, .Top, Me.ScaleWidth - (.Width + 100), .Height
139. End With
140. End Sub