各位大侠:从DBGRID中查询到的数据内容,怎么样才能导出到EXCEL中!

sstlwg 2004-09-07 01:26:49
各位大侠:从DBGRID中查询到的数据内容,怎么样才能导出到EXCEL中!
...全文
86 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ajianchen2002 2004-09-08
请教楼上的大哥
我调用你的函数
为什么在 mGrid.MoveFirst 上报错
未找到数据方法或成员
请指教
回复
online 2004-09-07
使用了DataGrid,但是方法都差不多
private Sub GridTOexcel(mGrid As DataGrid)
Dim ColCount, i, k As Integer
Dim xlApp As New Excel.Application, xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet, sRange As String

ColCount = mGrid.Columns.Count

xlApp.Visible = False

Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(3)
xlsheet.Visible = xlSheetHidden
Set xlsheet = xlBook.Worksheets(2)
xlsheet.Visible = xlSheetHidden '//隐藏sheet2
Set xlsheet = xlBook.Worksheets(1)
xlsheet.Name = "导出数据"
VB.Screen.MousePointer = vbHourglass
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, ColCount)).Merge
xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(2, ColCount)).Font.Size = 10

'//设置标题

For i = 0 To ColCount - 1
xlsheet.Columns(i + 1).ColumnWidth = mGrid.Columns(i).Width / 120
If mGrid.Columns(i).Visible = True Then
xlsheet.Cells(2, i + 1) = mGrid.Columns(i).Caption
End If
Next

mGrid.MoveFirst
i = 0
'//从网格到excel
While Not mGrid.EOF
xlsheet.Range(xlsheet.Cells(i + 3, 1), xlsheet.Cells(i + 3, ColCount)).Font.Size = 10
For k = 0 To ColCount - 1
If Not IsNull(mGrid.Columns(k).Value) Then
If mGrid.Columns(k).Visible = True Then
xlsheet.Cells(i + 3, k + 1) = CStr(mGrid.Columns(k).Value)
End If
End If
Next
mGrid.MoveNext
i = i + 1
Wend

'//关闭操作台
xlBook.SaveAs "D:\kk.xls"
xlBook.Close False
xlApp.Quit
Set xlApp = Nothing
VB.Screen.MousePointer = vbDefault
Msgbox "数据导出完毕!"
End Sub
回复
相关推荐
发帖

1188

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2004-09-07 01:26
社区公告
暂无公告