Ver Mensaje Individual
  #11 (permalink)  
Antiguo 24/05/2011, 12:28
Avatar de gasafonso
gasafonso
 
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 16 años, 7 meses
Puntos: 1
Respuesta: de Excel a msflexgrid

Código vb:
Ver original
  1. Option Explicit
  2.  
  3. ' \\ -- Autor :         Luciano Lodola -- http://www.recursosvisualbasic.com.ar/
  4.  
  5. ' ---------------------------------------------------------------------------------
  6.  
  7. ' \\ Declaraciones ( Apis )
  8. ' ------------------------------------------------------------------------------------------
  9. Private Declare Function SetErrorMode Lib "kernel32" (ByVal wMode As Long) As Long
  10. Private Declare Sub InitCommonControls Lib "Comctl32" ()
  11.  
  12.  
  13. ' -- Variables para acceder a la hoja excel
  14. Private obj_Excel       As Object
  15. Private obj_Workbook    As Object
  16. Private obj_Worksheet   As Object
  17.  
  18. Private Sub Form_Initialize()
  19.     Call SetErrorMode(2)
  20.     Call InitCommonControls
  21. End Sub
  22.  
  23. ' ----------------------------------------------------------------------------------
  24. ' \\ -- Inicio
  25. ' ----------------------------------------------------------------------------------
  26. Private Sub Form_Load()
  27.     Me.Caption = " Importar Excel a FlexGrid "
  28.     Command1.Caption = " Importar a Flexgrid "
  29.     ' -- Configurar el Grid
  30.    With MSFlexGrid1
  31.         .Cols = 10
  32.         .FixedCols = 0
  33.         .FormatString = "Fecha|Gastos"
  34.         .ColWidth(0) = 2000
  35.         .ColWidth(1) = 1500
  36.     End With
  37. End Sub
  38. ' ----------------------------------------------------------------------------------
  39. ' \\ -- Fin
  40. ' ----------------------------------------------------------------------------------
  41. Private Sub Form_Unload(Cancel As Integer)
  42.     Call Descargar
  43. End Sub
  44.  
  45. ' ----------------------------------------------------------------------------------
  46. ' \\ -- Función para leer los datos del Excel y cargarlos en el Flex
  47. ' ----------------------------------------------------------------------------------
  48. Private Function Excel_FlexGrid(sPath As String, FlexGrid As Object, Filas As Integer, Columnas As Integer, Optional sSheetName As String = vbNullString) As Boolean
  49.  
  50.     Dim i As Long
  51.     Dim n As Long
  52.    
  53.     On Error GoTo error_sub
  54.     ' -- Comproba si existe l archivo
  55.    If Len(Dir(sPath)) = 0 Then
  56.        MsgBox "No se ha encontrado el archivo: " & sPath, vbCritical
  57.        Exit Function
  58.     End If
  59.    
  60.     FlexGrid.Redraw = False
  61.    
  62.     Me.MousePointer = vbHourglass
  63.     ' -- crea rnueva instancia de Excel
  64.    Set obj_Excel = CreateObject("Excel.Application")
  65.     'obj_Excel.Visible = True
  66.    
  67.     ' -- Abrir el libro
  68.    Set obj_Workbook = obj_Excel.Workbooks.Open(sPath)
  69.     ' -- referencia la Hoja, por defecto la hoja activa
  70.    If sSheetName = vbNullString Then
  71.         Set obj_Worksheet = obj_Workbook.ActiveSheet
  72.     Else
  73.         Set obj_Worksheet = obj_Workbook.Sheets(sSheetName)
  74.     End If
  75.    
  76.     ' -- Setear Grid
  77.    With MSFlexGrid1
  78.         ' -- Especificar  la cantidad de filas y columnas
  79.        '.Cols = Columnas
  80.        .Rows = Filas
  81.         ' -- Recorrer las filas del FlexGrid para agregar los datos
  82.        For i = 1 To .Rows - 1
  83.             ' -- Establecer la fila activa
  84.            .Row = i
  85.             ' -- Recorrer las columnas del FlexGrid
  86.            For n = 0 To .Cols - 1
  87.                 ' -- Establecer columna activa
  88.                .Col = n
  89.                 ' -- Asignar a la celda del Flex el contenido de la celda del excel
  90.                .Text = obj_Worksheet.Cells(i + 1, n + 1).Value
  91.             Next
  92.         Next
  93.     End With
  94.     ' -- Cerrar libro
  95.    obj_Workbook.Close
  96.     ' -- Cerrar Excel
  97.    obj_Excel.quit
  98.     ' -- Descargar objetos para liberar recursos
  99.    Call Descargar
  100.     Excel_FlexGrid = True
  101.     FlexGrid.Redraw = True
  102. ' -- Errores
  103. Exit Function
  104. error_sub:
  105.     MsgBox Err.Description
  106.     Call Descargar
  107.     Me.MousePointer = vbDefault
  108.     FlexGrid.Redraw = True
  109. End Function
  110.  
  111. Private Sub HighLight_Cells(Grid As Object, cdblMinValue As Double, iCurrentCol As Integer)
  112.     Dim iRow As Integer
  113.     With Grid
  114.         .Redraw = False
  115.         For iRow = 1 To Grid.Rows - 1
  116.             ' -- Establecer Columna y fila activa
  117.            .Col = iCurrentCol
  118.             .Row = iRow
  119.            
  120.             ' -- Comparar el valor
  121.            If IsNumeric(.TextMatrix(iRow, iCurrentCol)) Then
  122.                 If CDbl(.TextMatrix(iRow, iCurrentCol)) >= cdblMinValue Then
  123.                     ' -- Remarcar y resaltar de color esta celda
  124.                    .CellForeColor = vbRed
  125.                 End If
  126.             End If
  127.         Next
  128.         .Redraw = True
  129.     End With
  130. End Sub

Esto es como lo tengo, casi original de Recursosvisuales