用統計按鈕查詢出來的資料然后點匯出提示424 要求物件
這個代碼是從論壇里找到的。請問下這個是哪里問題,我是VB小白
Private Sub Command2_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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/214309.html
標籤:控件
上一篇:請大佬幫忙,關于串口的問題。
