vb中datagrid的数据导出到excel文件
导出的数据超过几十行之后就开始显示重复的值,不知道是哪里出错了,请高手指点PrivateSubCommand2_Click()DimiAsIntegerDimjAsInt...
导出的数据超过几十行之后就开始显示重复的值,不知道是哪里出错了,请高手指点
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To mgrid.Columns.Count - 1
xlSheet.Cells(1, j + 1) = mgrid.Columns.Item(j).Caption
Next j
xlSheet.Cells(8, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
mgrid.Row = i
For j = 0 To mgrid.Columns.Count - 1
mgrid.Col = j
'MsgBox DataGrid1.Text
If IsNull(mgrid.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = mgrid.Text
End If
Next j
Next i
End Sub 展开
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To mgrid.Columns.Count - 1
xlSheet.Cells(1, j + 1) = mgrid.Columns.Item(j).Caption
Next j
xlSheet.Cells(8, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
mgrid.Row = i
For j = 0 To mgrid.Columns.Count - 1
mgrid.Col = j
'MsgBox DataGrid1.Text
If IsNull(mgrid.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = mgrid.Text
End If
Next j
Next i
End Sub 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询