Ver Mensaje Individual
  #7 (permalink)  
Antiguo 24/05/2011, 08:46
Avatar de lokoman
lokoman
 
Fecha de Ingreso: septiembre-2009
Mensajes: 502
Antigüedad: 15 años, 6 meses
Puntos: 47
Respuesta: de Excel a msflexgrid

Tengo el Office 2003, no actualizacion.
Simplemente le di COPY y PASTE al codigo del link, renombré los controles que te mencioné. No añadí referencias a nada, solo el componente FLEX.

• Agrega un MICROSOFT FLEXGRID CONTROL --> NOMBRE: MSFlexGrid
• Agrega un COMMAND --> NOMBRE: CMD
• En el evento clic del CMD:
Call Excel_FlexGrid("D:\Me\Down\New\Exe\Test.xls", MSFlexGrid, 20, 5, "Export")


Componentes


Referencias


Form sin datos


Form con datos

CODE:
Código vb:
Ver original
  1. Option Explicit
  2.  
  3. ' \\ -- Autor :         Luciano Lodola -- http://www.recursosvisualbasic.com.ar/
  4.  
  5. ' ---------------------------------------------------------------------------------
  6.  
  7.  
  8. ' -- Variables para acceder a la hoja excel
  9. Private obj_Excel       As Object
  10. Private obj_Workbook    As Object
  11. Private obj_Worksheet   As Object
  12.  
  13. ' ----------------------------------------------------------------------------------
  14. ' \\ -- Inicio
  15. ' ----------------------------------------------------------------------------------
  16. Private Sub Form_Load()
  17.     Me.Caption = " Importar Excel a FlexGrid "
  18.     cmd.Caption = " Importar a Flexgrid "
  19.     ' -- Configurar el Grid
  20.    With MSFlexGrid
  21.         .Cols = 10
  22.         .FixedCols = 0
  23.     End With
  24. End Sub
  25. ' ----------------------------------------------------------------------------------
  26. ' \\ -- Fin
  27. ' ----------------------------------------------------------------------------------
  28. Private Sub Form_Unload(Cancel As Integer)
  29.     Call Descargar
  30. End Sub
  31.  
  32. ' ----------------------------------------------------------------------------------
  33. ' \\ -- Función para leer los datos del Excel y cargarlos en el Flex
  34. ' ----------------------------------------------------------------------------------
  35. Private Sub Excel_FlexGrid(sPath As String, FlexGrid As Object, Filas As Integer, Columnas As Integer, Optional sSheetName As String = vbNullString)
  36.  
  37.     Dim i As Long
  38.     Dim n As Long
  39.    
  40.     On Error GoTo error_sub
  41.     ' -- Comproba si existe l archivo
  42.    If Len(Dir(sPath)) = 0 Then
  43.        MsgBox "No se ha encontrado el archivo: " & sPath, vbCritical
  44.        Exit Sub
  45.     End If
  46.    
  47.     Me.MousePointer = vbHourglass
  48.     ' -- crea rnueva instancia de Excel
  49.    Set obj_Excel = CreateObject("Excel.Application")
  50.     'obj_Excel.Visible = True
  51.    
  52.     ' -- Abrir el libro
  53.    Set obj_Workbook = obj_Excel.Workbooks.Open(sPath)
  54.     ' -- referencia la Hoja, por defecto la hoja activa
  55.    If sSheetName = vbNullString Then
  56.         Set obj_Worksheet = obj_Workbook.ActiveSheet
  57.     Else
  58.         Set obj_Worksheet = obj_Workbook.Sheets(sSheetName)
  59.     End If
  60.    
  61.     ' -- Setear Grid
  62.    With MSFlexGrid
  63.         ' -- Especificar  la cantidad de filas y columnas
  64.        '.Cols = Columnas
  65.        .Rows = Filas
  66.         ' -- Recorrer las filas del FlexGrid para agregar los datos
  67.        For i = 1 To .Rows - 1
  68.             ' -- Establecer la fila activa
  69.            .Row = i
  70.             ' -- Recorrer las columnas del FlexGrid
  71.            For n = 0 To .Cols - 1
  72.                 ' -- Establecer columna activa
  73.                .Col = n
  74.                 ' -- Asignar a la celda del Flex el contenido de la celda del excel
  75.                .Text = obj_Worksheet.Cells(i + 1, n + 1).Value
  76.             Next
  77.         Next
  78.     End With
  79.     ' -- Cerrar libro
  80.    obj_Workbook.Close
  81.     ' -- Cerrar Excel
  82.    obj_Excel.quit
  83.     ' -- Descargar objetos para liberar recursos
  84.    Call Descargar
  85. ' -- Errores
  86. Exit Sub
  87. error_sub:
  88.     MsgBox Err.Description
  89.     Call Descargar
  90.         Me.MousePointer = vbDefault
  91. End Sub
  92. ' ----------------------------------------------------------------------------------
  93. ' \\ -- Función para descargar los objetos
  94. ' ----------------------------------------------------------------------------------
  95. Private Sub Descargar()
  96.     On Local Error Resume Next
  97.     Set obj_Workbook = Nothing
  98.     Set obj_Excel = Nothing
  99.     Set obj_Worksheet = Nothing
  100.     Me.MousePointer = vbDefault
  101. End Sub
  102. ' ----------------------------------------------------------------------------------
  103. ' \\ -- Botón para leer los datos del libro e importarlos al Grid
  104. ' ----------------------------------------------------------------------------------
  105. Private Sub cmd_Click()
  106.     Call Excel_FlexGrid("D:\Me\Down\New\Exe\Test.xls", MSFlexGrid, 20, 5, "Export")
  107. End Sub