大家好我想把从SQL 中得到的数据保存到excel中,我现在可以保存了 也可以自己定义表名
及xls名,   但不知道如何使excel中的列名和数据集的相对应  。谢大家了

解决方案 »

  1.   

    '数据导出
    Dim excelsheet As Object
    Dim i As Integer
    Dim j As Integer
    Set excelsheet = CreateObject("excel.sheet")
    '数据库连接
    If conJxc.State = adStateClosed Then conJxc.Open "Provider=SQLOLEDB.1;Password=aa;Persist Security Info=True;User ID=aa;Initial Catalog=aa;Data Source=aa"
    '数据表
    If recTemp.State = adStateOpen Then recTemp.Close
        recTemp.Open "select a as 姓名 from a  order by a ", conJxc, adOpenStatic, adLockReadOnly, adCmdText    For i = 1 To recTemp.Fields.Count
            excelsheet.worksheets(1).cells(1, i).Value = recTemp.Fields(i - 1).Name
            recTemp.MoveFirst
            For j = 1 To recTemp.RecordCount
                excelsheet.worksheets(1).cells(j + 1, i).Value = recTemp.Fields(i - 1)
                Me.Caption = "现导出到:" & i & "/" & recTemp.Fields.Count & "/" & j
                recTemp.MoveNext
            Next
        
        Next
       
        
    excelsheet.saveas "c:\安装没交钱企业.xls"
    excelsheet.application.quit
    Set excelsheet = Nothing
    MsgBox "导出OK!"