Attribute VB_Name = "mExportarGridExcel"
'---------------------------------------------------------------------------------------
' Module    : Exportar Grid a Excel
' DateTime  : 24/10/2022 11:21
' Author    : Ivo Scavuzzo - SKLSOFT
' Purpose   : Para control ucGridPlus de Leandro Ascierto www.leandroascierto.com.ar
'---------------------------------------------------------------------------------------

Option Explicit

Public Function Exportar_Grid_Excel(gr As ucGridPlus, _
                                    Optional SheetName As String = "Hoja") As Boolean
    
    Dim iFila  As Long, iCol As Integer

    Dim oExcel As Object, oWBook As Object, oSheet As Object

    Set oExcel = CreateObject("Excel.Application")
    Set oWBook = oExcel.Workbooks.Add
    Set oSheet = oWBook.Worksheets(1)
    oSheet.Name = SheetName
    
    With oSheet
    
        For iCol = 0 To gr.ColsCount - 1
            
            'FILTRO POR SI HAY COLUMNAS OCULTAS
            If gr.ColHidden(iCol) = False Then
            
                'RELLENO NOMBRE COLUMNAS
                .Cells(1, iCol + 1) = gr.ColumnText(iCol)
                
                'ANCHO DE LA COLUMNA
                .Columns(UCase$(Chr(97 + iCol))).ColumnWidth = gr.ColWidth(iCol) / 6
            End If

        Next
            
        For iFila = 0 To gr.RowsCount - 1
            For iCol = 0 To gr.ColsCount - 1

                If gr.ColHidden(iCol) = False Then
                    .Cells(iFila + 2, iCol + 1) = gr.CellValue(iFila, iCol)
                End If
                
                If gr.ColDataType(iCol) = GP_DATE Then
                    .Cells(iFila + 2, iCol + 1).NumberFormat = "@"
                    .Cells(iFila + 2, iCol + 1) = gr.CellValue(iFila, iCol)
                    .Cells(iFila + 2, iCol + 1).NumberFormat = "@"
                    .Cells(iFila + 2, iCol + 1).NumberFormat = "dd/mm/yyyy"
                    .Cells(iFila + 2, iCol + 1).HorizontalAlignment = 1
                End If

            Next
        Next
    
    End With

    oExcel.Visible = True
    Set oExcel = Nothing
    
End Function

