Public Function ExportToExcelByFlexGrid(FLex As MSHFlexGrid, Merr As Boolean, Starrow As Long, Starcol As Long, title As String)
'------------------------------------------------
'功能:将MSHFlexGrid表中内容导出至Excel中
'参数:
' [Flex]................MSHFlexGrid表格
' [Merr]...............是否带合并单元格
' [Starrow]..............开始行
' [Starcol]..............开始列
' [title] ...............表头文字
'------------------------------------------------
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlApp.Workbooks(1).Worksheets(1)
On Error GoTo err
Screen.MousePointer = 13
With xlSheet
''''''''''''''''''''''
'如果不需要合并单位格'
''''''''''''''''''''''
If Merr = False Then
''''''''''
'导出数据'
''''''''''
For I = Starrow To FLex.Rows - 1
DoEvents
For J = Starcol To FLex.Cols - 1
.Cells(I + 2, J + 1).Value = FLex.TextMatrix(I, J)
Next
Next
''''''''''''''''''''
'加表头,调整表格式'
''''''''''''''''''''
.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, J)).Select
With xlApp.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
.Cells(1, 1) = title
Else
''''''''''''''''
'需要合并单元格'
''''''''''''''''
''''''''''
'导出数据'
''''''''''
For I = Starrow + 1 To FLex.Rows - 1
DoEvents
For J = Starcol To FLex.Cols - 1
.Cells(I + 2, J + 1).Value = FLex.TextMatrix(I, J)
Next
Next
'''''''''''''''''''''''''''''''
'写合并单元格的表头,表头第二行'
'''''''''''''''''''''''''''''''
For J = 3 To FLex.Cols Step 4
.Range(xlSheet.Cells(2, J), xlSheet.Cells(2, J + 3)).Select
With xlApp.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
.Cells(2, J) = FLex.TextMatrix(0, J)
Next
''''''''
'写标题'
''''''''
.Rows("1:1").RowHeight = 40
.Range(.Cells(1, 1), .Cells(1, FLex.Cols)).Select
With xlApp.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
.Cells(1, 1) = title
End If
''''''''''''''
'标题字体加粗'
''''''''''''''
.Range(.Cells(1, 1), .Cells(1, FLex.Cols)).Select
With xlApp.ActiveCell.Characters.Font
.Name = "宋体"
.FontStyle = "常规"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
End With
xlApp.Visible = True
Screen.MousePointer = 0
err:
If err.Number <> 0 Then
Screen.MousePointer = 0
MsgBox err.Description, vbInformation, "导出失败"
Exit Function
End If
End Function