Option Explicit
Private Const ROW_START As Long = 1
Public Sub ExportGrid(Grid As vbAcceleratorSGrid6.vbalGrid, ByVal bPrint As Boolean, Color_TOTAL_CELL_LIGHT As Long, Optional bUseCellValue As Boolean = False)
Dim lRow As Long
Dim lCol As Long
Dim xlApp As Excel.Application
Dim xlSht As Excel.Worksheet
Set xlApp = New Excel.Application
xlApp.Workbooks.Add
Set xlSht = xlApp.ActiveSheet
' Grid header-
For lCol = 1 To Grid.Columns
xlSht.Cells(ROW_START, lCol).Value = Grid.ColumnHeader(lCol)
Next
'Cell values
For lRow = 1 To Grid.Rows
For lCol = 1 To Grid.Columns
If bUseCellValue Then
xlSht.Cells(ROW_START + lRow, lCol).Value = Grid.CellItemData(lRow, lCol)
Else
xlSht.Cells(ROW_START + lRow, lCol).Value = Grid.CellText(lRow, lCol)
End If
Next
Next
' Draw gridlines
xlApp.Range(xlApp.Cells(ROW_START, 1), xlApp.Cells(ROW_START + Grid.Rows, Grid.Columns)).Select
With xlApp.Selection
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
If Grid.Columns > 1 Then
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
' Highlight the header using the gray color
xlApp.Range(xlApp.Cells(ROW_START, 1), xlApp.Cells(ROW_START, Grid.Columns)).Select
xlApp.Selection.Font.Bold = True
With xlApp.Selection.Interior
.ColorIndex = 15
End With
' Highlight T O T A L S line using the gray color
lRow = Grid.Rows '<== Watch out as the excel row is 1 greater than the Grid Row
If Grid.RowItemData(lRow) = -999999999 Then
'Make the left cell font color white, also the right cell (it may be 0 wide, but who cares)
xlApp.Range(xlApp.Cells(lRow + 1, 1), xlApp.Cells(lRow + 1, 1)).Select
xlApp.Selection.Font.Color = RGB(255, 255, 255)
xlApp.Range(xlApp.Cells(lRow + 1, Grid.Columns), xlApp.Cells(lRow + 1, Grid.Columns)).Select
xlApp.Selection.Font.Color = RGB(255, 255, 255)
'Now cycle along the cells, setting to dark or light backcolor
For lCol = 1 To Grid.Columns
If Grid.CellBackColor(lRow, lCol) = Color_TOTAL_CELL_LIGHT Then
xlApp.Range(xlApp.Cells(lRow + 1, lCol), xlApp.Cells(lRow + 1, lCol)).Select
xlApp.Selection.Font.Bold = True
xlApp.Selection.Interior.ColorIndex = 24
Else
xlApp.Range(xlApp.Cells(lRow + 1, lCol), xlApp.Cells(lRow + 1, lCol)).Select
xlApp.Selection.Font.Bold = True
xlApp.Selection.Interior.ColorIndex = 23 '24
End If
Next lCol
End If
'UNCOMMENT THE NEXT BLOCK TO SEE THE COLORINDEX COLORS
' ' Highlight a range with various ColorIndex values
' ' 15 = gray 24 = my light T O T A L S color 23 or 55 my (very) dark T O T A L S color
' Dim i As Integer
' For i = 1 To 56
' 'xlApp.Range(xlApp.Cells(ROW_START + 1, 1), xlApp.Cells(Grid.Rows, Grid.Columns)).Select
' xlApp.Range(xlApp.Cells(ROW_START + 1, i), xlApp.Cells(ROW_START + 1, i)).Select
' xlApp.Selection.Font.Bold = True
' With xlApp.Selection.Interior
' .ColorIndex = i
' '.Color = RGB(220, 220, 220) 'I may have to set one of the 52 colors then use that
' '.pattern = xlPatternGray50 'xlPatternGray25 'xlSolid
' End With
' Next i
'Size columns and rows
For lCol = 1 To Grid.Columns
If Grid.ColumnVisible(lCol) = True Then
xlApp.Columns(lCol).ColumnWidth = Grid.ColumnWidth(lCol) / 6
Else
xlApp.Columns(lCol).ColumnWidth = 0
End If
Next
For lRow = 1 To Grid.Rows + 1
xlSht.Rows(lRow).RowHeight = 15
Next
If bPrint Then
' Print
xlApp.ActiveWindow.SelectedSheets.PrintOut
xlApp.ActiveWindow.Close SaveChanges:=False
Else
' Displaying Excel
xlApp.Cells(1, 1).Select
xlApp.Visible = True
End If
DoEvents
Set xlSht = Nothing
Set xlApp = Nothing
End Sub
' 1440 Twips = 1 logical Inch
' 72 Points = 1 logical Inch
' Convert my Pixels to Twips then * 72 and / 1440
Private Function ConvertPixelsToPoints(lIN As Long) As Long
Dim myLng As Long
myLng = lIN / 6
ConvertPixelsToPoints = myLng
End Function