Private Sub export_cmd_Click()
Dim r As Integer, c As Integer
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
Set newxls = CreateObject("excel.application")
Set newbook = newxls.Workbooks.Add
Set newsheet = newbook.Worksheets(1)
If khxxcx_data.Recordset.RecordCount > 0 Then
newxls.Visible = True
For i = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(1, i + 1) = khxxcx_datagrid.Columns(i).Caption
Next i
khxxcx_data.Recordset.MoveFirst
Do Until khxxcx_data.EOFAction
r = khxxcx_data.Recordset.AbsolutePosition
For c = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(r + 1, c + 1) = khxxcx_datagrid.Columns(c)
Next c
khxxcx_data.Recordset.MoveNext
Loop
End If
Set newsheet = Nothing
Set newbook = Nothing
Set newxls = Nothing
newxls.Quit
End Sub
运行后就出现那样的错误“应用程序定义或对象定义错误”,但是还是能把数据导出到EXCEL中
请高手帮帮忙
俺已经搞定了,不过还要谢谢你们,下面是我的答案:
Private Sub export_cmd_Click()
Dim r As Integer, c As Integer
Dim newxls As Variant
Dim newbook As Variant
Dim newsheet As Variant
Set newxls = CreateObject("excel.application")
Set newbook = newxls.Workbooks.Add
Set newsheet = newbook.Worksheets(1)
Set newsheet = newbook.ActiveSheet
If khxxcx_data.Recordset.RecordCount > 0 Then
newxls.Visible = True
For i = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(1, i + 1) = khxxcx_datagrid.Columns(i).Caption
Next i
khxxcx_data.Recordset.MoveFirst
Do Until khxxcx_data.Recordset.EOF
r = khxxcx_data.Recordset.AbsolutePosition
For c = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(r + 1, c + 1) = khxxcx_datagrid.Columns(c)
Next c
khxxcx_data.Recordset.MoveNext
Loop
End If
newxls.Quit
Set newxls = Nothing
End Sub