Visual Basic - EXPORTAR A EXCEL DE UN LISTVIEW

Life is soft - evento anual de software empresarial
 
Vista:

EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por Jose de jesus (1 intervención) el 14/04/2005 04:36:02
POR FAVOR AYUDA, ALGUIEN QUE ME PASE UN MODULO O RUTINA PARA PODER EXPORTAR A EXCEL UN LISTVIEW PASANDOLE YO POR PARAMETROS EL TITULO Y LA FECHA Y QUE NO ME PASE LAS COLUMNAS OCULTAS????
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

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por DAviko (1 intervención) el 05/09/2006 01:40:32
yo tambien quiero
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por Cesar casanova (2 intervenciones) el 30/07/2008 11:58:45
Private Sub Button3_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim filepath As String = "TOTAL.xls"
Using file As New IO.StreamWriter(filepath, False)
Dim line As String = ""
For Each item As ListViewItem In Me.Lista.Items
line = ""
For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
line += subitem.Text + vbTab
Next
file.WriteLine(line)
Next
file.Close()
End Using
Process.Start(filepath)
End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por Dayana (2 intervenciones) el 08/04/2008 00:31:37
yo tambien necesito esto. gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por Sonia (2 intervenciones) el 11/05/2008 19:30:45
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
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por cesar casanova (2 intervenciones) el 30/07/2008 12:00:12
Private Sub Button3_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim filepath As String = "TOTAL.xls"
Using file As New IO.StreamWriter(filepath, False)
Dim line As String = ""
For Each item As ListViewItem In Me.Lista.Items
line = ""
For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
line += subitem.Text + vbTab
Next
file.WriteLine(line)
Next
file.Close()
End Using
Process.Start(filepath)
End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por javier (1 intervención) el 26/07/2009 03:50:21
Funciona bien este codigo en vb 2008 pero aparece un mensaje molesto que dice:

el archivo que intenta abrir "total.xls" tiene otro formato que el especificado por la extencion del archivo........etc....

tengo el office 2007 como se puede solucionar esto?????'
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:EXPORTAR A EXCEL DE UN LISTVIEW

Publicado por jose (1 intervención) el 31/01/2013 22:21:53
cambia en el codigo la extencion de ecxel por la que maneje tu office
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar