vb6.0将DataGrid1的数据导出excel
下面的代码是在网上找的,用了之后总是提示:对象关闭时,无法操作'汇出ExcelPrivateSubcmdexport_Click()Dimi,r,cAsIntegerDi...
下面的代码是在网上找的,用了之后总是提示:对象关闭时,无法操作
'汇出Excel
Private Sub cmdexport_Click()
Dim i, r, c As Integer
Dim newxls As New Excel.Application '声明application 对象
Dim newbook As New Excel.Workbook
Dim newsheet As New Excel.Worksheet
Set newbook = newxls.Workbooks.Add '创建工作薄
Set newsheet = newbook.Worksheets(1) '创建工作表
With CommonDialog1
.CancelError = False '在对话框中,按cancel键不出现错误
.Flags = cdlOFNHideReadOnly & cdlOFNOverwritePrompt
.Filter = "Excel Files (*.xls)|*.xls" '对话框底部“保存文件类型“下拉匡显示的内容"
.FilterIndex = 1 '选择"保存类型"的第一个值为默认值,即"Excel Files (*.xls)|*.xls"
.ShowSave
'设置Excel内容的格式,现在暂时不用
'With newsheet
' .Cells.Font.Name = "System"
' .Cells.Font.Size = 12
' .Name = "数据列表"
'
' Range(Cells(1, 1), Cells(1, DataGrid1.Columns.Count)).Select
' Selection.HorizontalAlignment = xlCenter
' Selection.VerticalAlignment = xlCenter
' Selection.Merge '合并居中
' .Cells(1, 1) = "导出excel文件"
'End With
If rs.RecordCount > 0 Then
For i = 0 To DataGrid1.Columns.Count - 1 '循环添加表头
newsheet.Cells(2, i + 1) = DataGrid1.Columns(i).Caption
Next
rs.MoveFirst
Do Until rs.EOF
r = rs.AbsolutePosition '读取recordset对象当前记录的序号位置
For c = 0 To DataGrid1.Columns.Count - 1
DataGrid1.col = c '读取地c列数据
newsheet.Cells(r + 2, c + 1) = DataGrid1.Columns(c)
Next
rs.MoveNext
Loop
End If
newsheet.SaveAs FileName:=CommonDialog1.FileName
newbook.Close
newxls.Quit
Set newbook = Nothing
Set newsheet = Nothing
Set newxls = Nothing
Set conn = Nothing
Set rs = Nothing
End With
MsgBox "数据导出成功", vbMsgBoxRight, "提示"
End Sub 展开
'汇出Excel
Private Sub cmdexport_Click()
Dim i, r, c As Integer
Dim newxls As New Excel.Application '声明application 对象
Dim newbook As New Excel.Workbook
Dim newsheet As New Excel.Worksheet
Set newbook = newxls.Workbooks.Add '创建工作薄
Set newsheet = newbook.Worksheets(1) '创建工作表
With CommonDialog1
.CancelError = False '在对话框中,按cancel键不出现错误
.Flags = cdlOFNHideReadOnly & cdlOFNOverwritePrompt
.Filter = "Excel Files (*.xls)|*.xls" '对话框底部“保存文件类型“下拉匡显示的内容"
.FilterIndex = 1 '选择"保存类型"的第一个值为默认值,即"Excel Files (*.xls)|*.xls"
.ShowSave
'设置Excel内容的格式,现在暂时不用
'With newsheet
' .Cells.Font.Name = "System"
' .Cells.Font.Size = 12
' .Name = "数据列表"
'
' Range(Cells(1, 1), Cells(1, DataGrid1.Columns.Count)).Select
' Selection.HorizontalAlignment = xlCenter
' Selection.VerticalAlignment = xlCenter
' Selection.Merge '合并居中
' .Cells(1, 1) = "导出excel文件"
'End With
If rs.RecordCount > 0 Then
For i = 0 To DataGrid1.Columns.Count - 1 '循环添加表头
newsheet.Cells(2, i + 1) = DataGrid1.Columns(i).Caption
Next
rs.MoveFirst
Do Until rs.EOF
r = rs.AbsolutePosition '读取recordset对象当前记录的序号位置
For c = 0 To DataGrid1.Columns.Count - 1
DataGrid1.col = c '读取地c列数据
newsheet.Cells(r + 2, c + 1) = DataGrid1.Columns(c)
Next
rs.MoveNext
Loop
End If
newsheet.SaveAs FileName:=CommonDialog1.FileName
newbook.Close
newxls.Quit
Set newbook = Nothing
Set newsheet = Nothing
Set newxls = Nothing
Set conn = Nothing
Set rs = Nothing
End With
MsgBox "数据导出成功", vbMsgBoxRight, "提示"
End Sub 展开
3个回答
展开全部
你的conn没有设置,就是把数据库和RS连接的部分,你要先打开数据库,然后才能操作数据,因为你这里只是部分代码,只能这样推断
更多追问追答
追问
嗯 我打开了数据库了,但是我导出的数据重复的,一直重复第一条数据,第二条以后的就没有导出来了,请问这是怎么回事啊?
追答
你的DataGrid1里面的数据源跟rs不是一致的吧,你应该操作那个跟DataGrid1绑定在一起的数据控件,而不是操作rs,我现在没有用VB6,没办法帮你调试,你试下,不行的话再留言
展开全部
With CommonDialog1
.CancelError = False '在对话框中,按cancel键不出现错误
.Flags = cdlOFNHideReadOnly & cdlOFNOverwritePrompt
.Filter = "Excel Files (*.xls)|*.xls" '对话框底部“保存文件类型“下拉匡显示的内容"
.FilterIndex = 1 '选择"保存类型"的第一个值为默认值,即"Excel Files (*.xls)|*.xls"
.ShowSave
end with 少了这句吧
newsheet.SaveAs CommonDialog1.FileName 'saveas后面应该跟文件路径名字,看你跟的是什么
.CancelError = False '在对话框中,按cancel键不出现错误
.Flags = cdlOFNHideReadOnly & cdlOFNOverwritePrompt
.Filter = "Excel Files (*.xls)|*.xls" '对话框底部“保存文件类型“下拉匡显示的内容"
.FilterIndex = 1 '选择"保存类型"的第一个值为默认值,即"Excel Files (*.xls)|*.xls"
.ShowSave
end with 少了这句吧
newsheet.SaveAs CommonDialog1.FileName 'saveas后面应该跟文件路径名字,看你跟的是什么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询