将VB6.0 datagrid的数据导出到EXCEL表,只导出了标题行???
OptionExplicitDimi,j,kAsIntegerDimxlappAsVariantDimxlBookAsVariantDimxlSHEETAsVariant...
Option Explicit
Dim i, j, k As Integer
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlSHEET As Variant
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
Set xlapp = CreateObject("excel.application")
Set xlBook = xlapp.Workbooks.Add
Set xlSHEET = xlBook.Worksheets(1)
xlapp.Visible = True
On Error Resume Next
If Err.Number <> 0 Then Set xlapp = CreateObject("Excel.Application")
Set xlBook = xlapp.Workbooks.Add
Set xlSHEET = xlBook.ActiveSheet
For k = 1 To DataGrid1.Columns.Count
xlSHEET.Cells(1, k) = DataGrid1.Columns(k - 1).Caption
Next k
For i = 1 To rs.RecordCount + 1
For j = 0 To DataGrid1.Columns.Count
xlSHEET.Cells(i + 1, j + 1) = rs(j)
Next j
rs.MoveNext
Next i
End Sub 展开
Dim i, j, k As Integer
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlSHEET As Variant
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
Set xlapp = CreateObject("excel.application")
Set xlBook = xlapp.Workbooks.Add
Set xlSHEET = xlBook.Worksheets(1)
xlapp.Visible = True
On Error Resume Next
If Err.Number <> 0 Then Set xlapp = CreateObject("Excel.Application")
Set xlBook = xlapp.Workbooks.Add
Set xlSHEET = xlBook.ActiveSheet
For k = 1 To DataGrid1.Columns.Count
xlSHEET.Cells(1, k) = DataGrid1.Columns(k - 1).Caption
Next k
For i = 1 To rs.RecordCount + 1
For j = 0 To DataGrid1.Columns.Count
xlSHEET.Cells(i + 1, j + 1) = rs(j)
Next j
rs.MoveNext
Next i
End Sub 展开
展开全部
根本不需要这么啰嗦,只需要一行代码就可以解决
XlApp.Workbooks(1).Worksheets(1).Cells(1, 1).CopyFromRecordset DataGrid1.DataSource
XlApp.Workbooks(1).Worksheets(1).Cells(1, 1).CopyFromRecordset DataGrid1.DataSource
更多追问追答
追问
我是新手 不太明白 能说的具体点吗 我把这句话替换了我代码中循环的那部分 可是导出的EXCEL表是空的。
追答
Private Sub Command1_Click()
Dim xlapp As Variant
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
XlApp.Workbooks(1).Worksheets(1).Cells(1, 1).CopyFromRecordset DataGrid1.DataSource
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询