请问 为什么我的datagrid导出excel只有一列
我想把显示在datagrid 的数据导出至excel
可是不知道什么原因 导出的时候只有一列是第一列 而且 有两个excel表
请问 我错在那里了 代码如下
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 dgrecord.Columns.Count
xlsheet.cells(1, k) = dgrecord.Columns(k - 1).Caption
Next k
For i = 1 To RsQRecord.RecordCount + 1
For j = 0 To dgrecord.Columns.Count
xlsheet.cells(i + 1, j + 1) = RsQRecord(j)
xlsheet.cells(i + 1, j + 1) = dgrecord.Columns(j).CellText(dgrecord.RowBookmark(j))
Next j
RsQRecord.MoveNext
Next i
[解决办法]
支持楼上
并且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
这里好象有问题 或许是你出现两个Excel表的原因???
[解决办法]
Private Sub cmnexcel_Click()
Set xlapp = CreateObject( "excel.application ")
Set xlbook = xlapp.workbooks.Add
Set xlsheet = xlbook.worksheets(1)
On Error Resume Next
If Err.Number <> 0 Then Set xlapp = CreateObject( "Excel.Application ")
Set xlbook = xlapp.workbooks.Add
Set xlsheet = xlbook.Worksheets( "Sheet1 ")
xlsheet.cells.CopyFromRecordset RsQRecord
xlsheet.Rows(1).Insert
For k = 1 To dgrecord.Columns.Count
xlsheet.cells(1, k) = dgrecord.Columns(k - 1).Caption
Next k
xlapp.Visible = True
End Sub