Disculpa las molestias y ojala puedas ayudarme, necesito exportar el contenido de un datagrid a excel, el datagrid se encuentras enlazado a un dataenvironment. Gracias por ayudar
Supongo que lo que quieres es exportar el recordset que esta asociado con el data grid, ¿es asi? Te envio un ejemplo en el cual paso como parametro tanto el recordset como el grid, esto porque asi lo requiere mi aplicacion. Tu lo puedes manipuar como quieras. Debes incluir una referencia a Microsoft Excel Object Library. Public Sub ExportRecordsetToExcel(rs As ADODB.Recordset, Grid As TDBGrid, Salida As String) Dim xlApp As Excel.Application Dim xlbook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim fila As Long Dim i As Long On Error GoTo ExportaExcel_Error Screen.MousePointer = vbHourglass Set xlApp = New Excel.Application Set xlbook = xlApp.Workbooks.Add xlbook.SaveAs Salida Set xlSheet = xlbook.Worksheets(1) fila = 1 'Imprimir la cabecera For i = 0 To Grid.Columns.Count - 1 xlSheet.Cells(fila, i + 1).Value = Grid.Columns(i).Caption Next i If Not (rs.EOF And rs.BOF) Then 'checar recordset vacio 'Imprimir el resto del reporte fila = fila + 1 rs.MoveFirst While Not rs.EOF For i = 0 To Grid.Columns.Count - 1 xlSheet.Cells(fila, i + 1).Value = rs.Fields(Grid.Columns(i).DataField).Value Select Case Grid.Columns(i).NumberFormat Case "Currency" xlSheet.Cells(fila, i + 1).NumberFormat = EXCEL_MONEDA Case "" xlSheet.Cells(fila, i + 1).NumberFormat = EXCEL_STRING Case "General Number" xlSheet.Cells(fila, i + 1).Value = Format(xlSheet.Cells(fila, i + 1).Value, "##,##0.00") xlSheet.Cells(fila, i + 1).NumberFormat = EXCEL_DECIMAL2 End Select Next i rs.MoveNext fila = fila + 1 Wend End If xlbook.Save 'xlbook.Close xlApp.Visible = True 'xlApp.Quit Set xlApp = Nothing Set xlbook = Nothing Set xlSheet = Nothing Screen.MousePointer = vbNormal GoTo Fin ExportaExcel_Error: xlApp.Quit Set xlApp = Nothing Set xlbook = Nothing Set xlSheet = Nothing Fin: End Sub