下面的代码是在网上找的,用了之后总是提示:对象关闭时,无法操作
'汇出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
嗯 我打开了数据库了,但是我导出的数据重复的,一直重复第一条数据,第二条以后的就没有导出来了,请问这是怎么回事啊?
追答你的DataGrid1里面的数据源跟rs不是一致的吧,你应该操作那个跟DataGrid1绑定在一起的数据控件,而不是操作rs,我现在没有用VB6,没办法帮你调试,你试下,不行的话再留言
追问可以了,谢谢你哦!^-^
vb6.0里datagrid里的数据怎么导到excel里?
newsheet.Cells(r + 1, c + 1) = DataGrid1.Columns(c)Next c Adodc1.Recordset.MoveNext Loop Dim myval As Long Dim mystr As String myval = MsgBox("是否保存该Excel表?", vbYesNo, "提示窗口")If myval = vbYes Then mystr = InputBox("请输入文件名称", "输入窗口")If Len...
VB6.0的DATAGRID数据导出EXCEL
Conn.Execute "insert into [excel 8.0;database=" & Filepath & "].[S_Line_GE$] select 回路编号,顶点序号,东坐标,北坐标,高程,回路电压,回路导线,里程,转角,方位角,控制区段,杆塔编号,杆塔型号 from S_Line_GE order by 回路编号,里程"'将库中的数据导出到刚刚建立的表中。看上去多...
VB高手请进.使用VB6.0的DATAGRID控件输出到EXCEL里时只显示可见行,请问...
oSheet.Range(strColName1).Value = Me.DataGrid1.Columns(i).Caption '据DataGrid中的宽度设置EXCEL列宽 StrColName = Chr(65 + i) & ":" & Chr(65 + i)oSheet.Columns(StrColName).ColumnWidth = Int(Me.DataGrid1.Columns(i).Width \/ 90)Next i '输出内容 Do Until Me.Adodc1.Recor...
将VB6.0 datagrid的数据导出到EXCEL表,只导出了标题行???
XlApp.Workbooks(1).Worksheets(1).Cells(1, 1).CopyFromRecordset DataGrid1.DataSource
vb6.0中datagrid导出excel数据设置文本格式?
xlSheet.Cells(j + 1, i + 1) = 用户_进口Grid.Columns.Item(i).Text 后面来一句:xlSheet.Selection.NumberFormatLocal = "@"
vb 关于生成excel表格
材料\/工具:电脑、VB 6.0软件 1、首先,我们需要点击文件菜单栏按钮。2、然后,我们点击生成EXE按钮。3、接下来,我们点击保存路径。4、然后,我们输入我们想要保存的文件名。5、最后,我们点击确定按钮。6、然后,我们便可以看到,在桌面生成了Excel表格了。
VB6.0 连接Access数据库后 如何编写代码使数据库导出到excel、word或htm...
Private Sub Command1_Click() Dim xlapp As Variant Dim xlBook As Variant Dim xlSheet As Variant Dim sum As Long Set xlapp = CreateObject("excel.application") Set xlBook = xlapp.Workbooks.Open(App.Path & "\\data\\报表.xlt") '打开EXCEL模板 'Set xlBook =...
vb6.0中如何将datagrid打印到word上
atable.Cell(DataGrid1.Bookmark + 1, 3).Range.InsertAfter Adodc1.Recordset.Fields("规格型号")If Adodc1.Recordset.Fields("批号或炉号") <> "" Then atable.Cell(DataGrid1.Bookmark + 1, 4).Range.InsertAfter Adodc1.Recordset.Fields("批号或炉号")atable.Cell(DataGrid1.Bookmark + ...
VB6.0 怎样取出DataGrid中单元格的内容,并把它赋值给变量a?
a = DataGrid1.Columns(Index).CellText(Bookmark)或者是:datagrid1.Row=ROW '选中某一行 datagrid1.col=COL '选中某一列 a=datagrid1.text
VB6.0中如何用按钮对DataGrid中的记录进行操作
sql = "select * from 超级用户表 where 用户名 = '" & Trim$(Text1.Text) & "' "Set con = New ADODB.Connection Set rs = New ADODB.Recordset cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\k01\\桌面\\耿会\\数据源信息汇总.mdb;Persist Security...