在VB中,怎样把Mshflexgrid显示的内容存成一个EXCEL文件(急)

simenh 2003-10-25 02:20:23
诸位高手:

请帮助小弟,怎样把Mshflexgrid显示的内容存成一个EXCEL文件。
方便的话,附上代码。

多谢
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
laibagefei 2003-11-29
  • 打赏
  • 举报
回复
我要,谢谢冰
igaoyu@eyou.com
icedut 2003-11-28
  • 打赏
  • 举报
回复
我有个例子,你要么
ZQY801105 2003-11-28
  • 打赏
  • 举报
回复
这回应该满足了吧,记得给分呀:
以下为直接一个一个写到Excel

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
Dublue 2003-10-25
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2388/2388211.xml?temp=.9988825

807

社区成员

发帖
与我相关
我的任务
社区描述
VB 多媒体
社区管理员
  • 多媒体
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧