Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/04/2006, 13:25
Avatar de wiro
wiro
 
Fecha de Ingreso: abril-2006
Mensajes: 72
Antigüedad: 18 años, 1 mes
Puntos: 0
No entiendo mucho lo que quieres con eso de exportar un datagrid a excel, pero bueno creo que lo que quieres es exportar el resultado de una consulta en donde el resultado lo estas vaciendo en un datagrid.

para ello puedes usar esta funcion a me me resulta muy bien.. espero y te sirva.

Public Sub Pase_Excel(Str_Sql As String, strConex As String)

Dim Int_Columnas As Integer
Dim Int_Filas As Integer
Dim rs_main As New ADODB.Recordset
Dim excelApp As Excel.Application
Dim excellibro As Excel.Workbook
Dim excelhoja As Excel.Worksheet
Dim Conn As ADODB.Connection

Set Conn = New ADODB.Connection
Conn.ConnectionString = strConex
Conn.Open

With rs_main
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockBatchOptimistic
Set .ActiveConnection = Conn
.Open Str_Sql
End With

Set excelApp = New Excel.Application
Set excellibro = excelApp.Workbooks.Add
Set excelhoja = excellibro.ActiveSheet
Int_Columnas = rs_main.Fields.Count

For i = 1 To Int_Columnas
excelhoja.Cells(1, i) = rs_main.Fields(i - 1).Name
Next

If rs_main.RecordCount > 0 Then
rs_main.MoveFirst
For Int_Filas = 1 To rs_main.RecordCount
For j = 0 To Int_Columnas - 1
If IsNull(rs_main(j).Value) Then
excelhoja.Cells(Int_Filas + 2, j + 1) = ""
Else
excelhoja.Cells(Int_Filas + 2, j + 1) = CStr(rs_main(j).Value)
End If
Next
rs_main.MoveNext
Next
End If

excelApp.Visible = True

End Sub