Visual Basic - EXPORTAR NOMBRES DE CAMPOS A EXCEL

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil
Val: 18
Ha mantenido su posición en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

EXPORTAR NOMBRES DE CAMPOS A EXCEL

Publicado por Pablo (18 intervenciones) el 31/01/2019 14:53:13
Amigos. Por favor sálvenme el pellejo una vez más.
Tengo un código que exporta un datagrid a Excel. Lo hace fantástico salvo por un detalle: Me exporta los datos,pero no el nombre de los campos. ¿Pueden ayudarme con eso?

Gracias de antemano.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Set cn5 = New ADODB.Connection
Set rst5 = New ADODB.Recordset
 
Dim xl As Object
Dim wbk As Object
 
Set xl = CreateObject("Excel.Application")
xl.Visible = True
 
Set rst5.DataSource = DataGrid1.DataSource
If Not rst5.EOF Then
    Set wbk = xl.Workbooks.Add
    wbk.Sheets(1).Range("A1").Select
    wbk.ActiveSheet.Cells.CopyFromRecordset rst5
End If
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
sin imagen de perfil
Val: 355
Bronce
Ha mantenido su posición en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

EXPORTAR NOMBRES DE CAMPOS A EXCEL

Publicado por raul (160 intervenciones) el 31/01/2019 15:34:15
basicamente tu codigo requiere 2 partes de las que solo tu hicistes la segunda.

Si te fijas estas mandando la información que contiene el recordset o sea los registros o sea que primero necesitas una rutina para copiar las columnas del datagrid al excel y luego inicializar tu codigo en la segunda linea

más o menos así. supon que db1 es el datagrid

1
2
3
4
dim bucle as integer
for bucle = 0 to db1.columns.count
wbk.Sheets(1).cells(1, bucle).value = db1.colunms(bucle).caption
next bucle

luego en tu parte del codigo cambias esta linea
1
wbk.Sheets(1).Range("A2").Select
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
sin imagen de perfil
Val: 18
Ha mantenido su posición en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

EXPORTAR NOMBRES DE CAMPOS A EXCEL

Publicado por Pablo (18 intervenciones) el 31/01/2019 15:41:57
Es esta línea.

1
wbk.Sheets(1).CELLS(1, bucle).Value = DataGrid1.Columns(bucle).Caption

Me arroja este error:

Object variable or with block variable not set

Esye es mi código completo:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Set cn5 = New ADODB.Connection
Set rst5 = New ADODB.Recordset
 
Dim xl As Object
Dim wbk As Object
Dim CELLS As Object
 
 
 
 
Dim bucle As Integer
For bucle = 0 To DataGrid1.Columns.Count
wbk.Sheets(1).CELLS(1, bucle).Value = DataGrid1.Columns(bucle).Caption
Next bucle
 
Set xl = CreateObject("Excel.Application")
xl.Visible = True
 
Set rst5.DataSource = DataGrid1.DataSource
If Not rst5.EOF Then
    Set wbk = xl.Workbooks.Add
    wbk.Sheets(1).Range("A2").Select
    wbk.ActiveSheet.CELLS.CopyFromRecordset rst5
End If
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