将datagrid中记录导出到EXECL时出错!
在我的datagrid中共有34条记录,当我执行下列语句导出到EXECL时,记录被不连续导出,不知何因?
Private Sub 导出成excel格式_Click()
sql = "select * from 报销 "
Call dakailianjie(sql)
rstmember.Sort = "No "
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 = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open( "d:\text2.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
rstmember.MoveFirst
For i = 0 To rstmember.RecordCount - 1
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
End Sub
导出结果:
No日期单据
12007-3-261
22007-3-262
32007-3-263
42007-3-291
52007-3-291
62007-3-291
72007-3-301
82007-3-301
92007-3-301
102007-4-21
112007-4-21
122007-4-21
132007-4-21
142007-4-21
152007-4-21
172007-4-31
202007-4-111
242007-4-121
292007-4-131
[解决办法]
你完全没必要这样导的
就一句话,还N快
创建了excel的sheet以后
mysheet.Cells.CopyFromRecordset Recordset
这样就把datagrid的rs的记录都放到excel的sheet里面了