标题:怎么将DATAGRID数据导入到EXCEL
只看楼主
gggg007
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2007-7-18
 问题点数:0 回复次数:4 
怎么将DATAGRID数据导入到EXCEL

各位先人
请给个实例或相关贴子
本人比较菜 最好是易懂点的

搜索更多相关主题的帖子: EXCEL DATAGRID 数据 先人 实例 
2007-08-27 10:42
shpilips
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-8-18
得分:0 

我比代码你:
Dim xlapp As Object '定义EXCEL类
Dim xlbook As Object '定义工件簿类
Dim xlsheet As Object '定义工作表类
Dim N As Integer
Screen.MousePointer = vbHourglass
Sjdf.Show
Sjdf.Label1.Caption = "正在导出数据,请稍后..."
Dim i As Long, j As Long, k As Long
If SFileName = "" Then
GridToExcel = False
Exit Function
End If
On Error GoTo err:
Set xlapp = CreateObject("Excel.Application")
If err Then
MsgBox "您的系统没有安装Execl文档!!!", 0 + 48, "提示"
Exit Function
End If
Set xlbook = CreateObject("Excel.Sheet")
Set xlbook = xlapp.Workbooks.Add 'Open(SFileName)
xlapp.Visible = False
Set xlsheet = xlbook.Worksheets(1) '设置sheet1,2,3项

DGrid.Scroll 0, -DGrid.FirstRow '定位到第一行
DGrid.row = 0
For k = 0 To DGrid.Columns.Count - 1 'DataGrid所有的列数
xlsheet.Cells(1, k + 1) = Trim(DGrid.Columns(k).Caption) '第一行为DataGrid的列标题
Next
Sjdf.PBar1.Max = DGrid.ApproxCount '进度条最大值不能等于0
For i = 0 To DGrid.ApproxCount - 1 'DataGrid的所有行数
DoEvents
For j = 0 To DGrid.Columns.Count - 1
If DGrid.Columns(0).Text = "" And i = 0 Then
Screen.MousePointer = vbDefault
Unload Sjdf
GoTo Dispose:
Else
xlsheet.Cells(i + 2, j + 1) = DGrid.Columns(j).Text '从第二行显示'DataGrid的内容
End If
Next j
If Sh = "opf" Or Si = "ipf" Then
If Oxls = True Then
If DGrid.ApproxCount < 16 Then
N = DGrid.ApproxCount - 1
Else
N = DGrid.ApproxCount
End If
Else
N = DGrid.ApproxCount - 1
End If
Else
N = DGrid.ApproxCount - 1
End If
If i < N Then
DGrid.row = DGrid.row + 1
End If
Sjdf.PBar1.value = i
Next i
DGrid.Scroll 0, -DGrid.FirstRow '定位到第一行
Screen.MousePointer = vbDefault
Unload Sjdf
MsgBox "数据导出完毕!", vbOKOnly + 48, "提示"
xlbook.SaveAs (SFileName)
GridToExcel = True
GoTo Dispose:
err:
Screen.MousePointer = vbDefault
MsgBox "存为EXCEL文件时出错!未能导出数据。", vbOKOnly + 48, "提示"
Unload Sjdf
GridToExcel = False
GoTo Dispose:
Dispose:
xlbook.Saved = True
xlbook.Close
xlapp.Quit
Set xlapp = Nothing
Set xlbook = Nothing


2007-08-27 22:18
gggg007
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2007-7-18
得分:0 
回复:(gggg007)怎么将DATAGRID数据导入到EXCEL

谢谢了

2007-08-28 07:43
dazuiaiwan
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-3-17
得分:0 
想学习VB是不是有点落伍了,把VB当作编程入门,怎么样??
2008-03-28 14:06
xiecaijian
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-24
得分:0 
我也想学习VB,只是本人是个编程盲,
特别害怕编程,以后还请多多指教啊.

从不奢求生活能给予我最好的,只是执着于寻求最适合我的.
2008-03-28 14:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-165675-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.467571 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved