奇怪,就是不显示第一列“姓名”别的都正常,代码是书上的,大家帮看看。由于急着用,所以 高分 求助,谢谢大家了Private Sub Command8_Click()Dim str As Variant
Dim str1 As VariantIf Adodc1.Recordset.EOF Then
Exit Sub
End If
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As IntegerSet xlApp = CreateObject("excel.application")
Dim xlBook As Object
Dim xlSheet As ObjectxlApp.Visible = True
Set xlBook = xlApp.Workbooks.AddSet xlSheet = xlBook.Worksheets(1)j = DataGrid1.Columns.Count
i = 1
For n = 1 To j - 1If DataGrid1.Columns(n).Visible = True ThenxlSheet.cells(2, i) = DataGrid1.Columns(n).Captioni = i + 1
End If
Next n
Adodc1.Recordset.MoveFirst
m = 0
Do While Not Adodc1.Recordset.EOF
i = 1
For n = 1 To j - 1If DataGrid1.Columns(n).Visible = True ThenxlSheet.cells(m + 3, i) = DataGrid1.Columns(n).Valuei = i + 1
End If
Next n
Adodc1.Recordset.MoveNext
m = m + 1
Loop
Exit SubEnd Sub

解决方案 »

  1.   

    教科书害人啊 居然去读datagrid去导第一,最好学习ado,而非adodc控件.
    第二,datagrid并非真实的网格控件,而是Recordset的反映.你那书本真是老土得掉牙了,也不知道谁写的那破代码.当确定第二点后,你就可知道只要直接把绑定datagrid的recordset导到excel中就可以了
    至于recordset理解吧.如果你用adodc控件,就是Adodc1.Recordset给你个导出模块,调用就是ExporToExcel"select * from tablename"这样样子Public Function ExporToExcel(strOpen As String)    Dim Rs_Data As New ADODB.Recordset
        Dim Irowcount As Integer
        Dim Icolcount As Integer    Dim xlApp As New Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim xlQuery As Excel.QueryTable    With Rs_Data
            If .State = adStateOpen Then
                .Close
            End If
            .ActiveConnection = Conn
            .CursorLocation = adUseClient
            .CursorType = adOpenStatic
            .LockType = adLockReadOnly
            .Source = strOpen
            .Open
        End With
        With Rs_Data
            If .RecordCount < 1 Then
                MsgBox ("没有记录!")
                Exit Function
            End If
            '记录总数
            Irowcount = .RecordCount
            '字段总数
            Icolcount = .Fields.Count
        End With    Set xlApp = CreateObject("Excel.Application")
        Set xlBook = Nothing
        Set xlSheet = Nothing
        Set xlBook = xlApp.Workbooks().Add
        Set xlSheet = xlBook.Worksheets("sheet1")
        xlApp.Visible = True    '添加查询语句,导入EXCEL数据
        Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))    With xlQuery
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = True
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
        End With    xlQuery.FieldNames = True '显示字段名
        xlQuery.Refresh    With xlSheet
            .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
            '设标题为黑体字
            .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
            '标题字体加粗
            .Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
            '设表格边框样式
        End With
        xlApp.Application.Visible = True
        Set xlApp = Nothing  '"交还控制给Excel
        Set xlBook = Nothing
        Set xlSheet = NothingEnd Function
      

  2.   

    另外,我特意去测试了你的导出代码,也没有什么问题.
    除了不是从第一行写起外.所以最好你重新来个数据库做一个检查
    注意:datagrid最好不要设置任何属性.