大家都知道,在数据库管理系统中(如各种财务软件,erp),几乎每个界面上都有GRID控件,用于显示数据表。但我目前遇到一个奇怪的问题:如果想把这个erp上的grid控件中的数据拷贝到excel中,结果有的行会出现错行的现象,类似如下: 姓名 年龄 工资
张三 20 1000
李四 21 1100
王二
22 1200
赵六 23 1300
请注意:上表中王二的数据发生了错行。我现在想,是否可以用vb,或其他程序编一个自已的拷贝函数,然后用他去直接拷贝erp界面上的grid控件中的数据到excel。不知是否可行?
张三 20 1000
李四 21 1100
王二
22 1200
赵六 23 1300
请注意:上表中王二的数据发生了错行。我现在想,是否可以用vb,或其他程序编一个自已的拷贝函数,然后用他去直接拷贝erp界面上的grid控件中的数据到excel。不知是否可行?
Dim i As Integer
Dim j As Integer
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\water.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
Adodc1.Recordset.MoveFirst
Bar1.Min = 0
Bar1.Max = Val(Adodc1.Recordset.RecordCount)
Bar1.Value = 0
For i = 0 To Adodc1.Recordset.RecordCount - 1
Bar1.Value = Bar1.Value + 1
DoEvents
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next j
Next i
Bar1.Value = 0
xlBook.Save
xlBook.Close
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
MsgBox ("本次共导出" & Adodc1.Recordset.RecordCount & "条记录!")
End Sub