怎么把SQL数据库查询到的数据导出到EXCEL或其它非SQL格式中>??论坛上的几种 方法我试过但是都无法导出.
  请教应该如何导出.是不是还需要一些其它控件什么的.我的导出语句要写在Private Sub Command2_Click()模块中
  有没有高手帮下忙啊.本人刚刚接触VB

解决方案 »

  1.   

    用vb可以直接引用excel进行导出
    或用第三方控件,推荐vsflexgrid8
      

  2.   

    导入Excel 
    SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="c:\test.xls";User ID=Admin;Password=;Extendedproperties=Excel 5.0')...xactions 
    SELECT cast(cast(科目编号 as numeric(10,2)) as nvarchar(255))+' ' 转换后的别名 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="c:\test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
      

  3.   

    谢谢各位我的已经解决,我用的语句是
      On Error Resume Next
    Dim irow, icol, count, i As Integer
    Dim irowcount, icolcount As Integer
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim bl As Boolean
    Dim key As IntegerDim RsUserTemp As Recordset
    Dim RsOrderTemp As Recordset
    Dim a, b
    Dim aa As String
    aa = Trim(Now)Set xlApp = CreateObject("excel.application")
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)'If rs.RecordCount < 1 Then
    'MsgBox ("Error 没有记录")
    'Exit Sub
    'End If
    irowcount = rs.RecordCount
    icolcount = 18
    count = 0
    rs.MoveFirst
    For icol = 0 To 2
    'xlSheet.Cells(1, 1).Value = "查询数据" '加标头;Next icol
    xlSheet.Cells(1, 1).Value = "时间" '加标头;
    xlSheet.Cells(1, 2).Value = "药开度" '加标头;
    xlSheet.Cells(1, 3).Value = "药瞬时流量" '加标头;
    xlSheet.Cells(1, 4).Value = "药累计流量" '加标头;
    xlSheet.Cells(1, 5).Value = "矿浆浓度" '加标头;
    xlSheet.Cells(1, 6).Value = "矿浆流量" '加标头
    xlSheet.Cells(1, 7).Value = "酸1开度" '加标头;
    xlSheet.Cells(1, 8).Value = "酸1瞬时流量" '加标头;
    xlSheet.Cells(1, 9).Value = "酸1累计流量" '加标头
    xlSheet.Cells(1, 10).Value = "酸2开度" '加标头;
    xlSheet.Cells(1, 11).Value = "酸2瞬时流量" '加标头;
    xlSheet.Cells(1, 12).Value = "酸2累计流量" '加标头
    xlSheet.Cells(1, 13).Value = "酸3开度" '加标头;
    xlSheet.Cells(1, 14).Value = "酸3瞬时流量" '加标头;
    xlSheet.Cells(1, 15).Value = "酸3累计流量" '加标头
    xlSheet.Cells(1, 16).Value = "酸4开度" '加标头;
    xlSheet.Cells(1, 17).Value = "酸4瞬时流量" '加标头;
    xlSheet.Cells(1, 18).Value = "酸4累计流量"
    xlSheet.Cells(1, 19).Value = "酸5开度"
    xlSheet.Cells(1, 20).Value = "酸5瞬时流量"
    xlSheet.Cells(1, 21).Value = "酸5累计流量"
    Adodc1.Recordset.MoveFirst
    For a = 2 To 200
       b = 1
       
       
      If Not Adodc1.Recordset.EOF Then
       xlSheet.Cells(a, b) = Adodc1.Recordset("时间")
       xlSheet.Cells(a, b + 1) = Adodc1.Recordset("药开度")
       xlSheet.Cells(a, b + 2) = Adodc1.Recordset("药瞬时流量")
       xlSheet.Cells(a, b + 3) = Adodc1.Recordset("药累计流量")
        xlSheet.Cells(a, b + 4) = Adodc1.Recordset("酸1开度")
         xlSheet.Cells(a, b + 5) = Adodc1.Recordset("酸1瞬时流量")
          xlSheet.Cells(a, b + 6) = Adodc1.Recordset("酸1累计流量")
           xlSheet.Cells(a, b + 7) = Adodc1.Recordset("酸2开度")
            xlSheet.Cells(a, b + 8) = Adodc1.Recordset("酸2瞬时流量")
             xlSheet.Cells(a, b + 9) = Adodc1.Recordset("酸2累计流量")
              xlSheet.Cells(a, b + 10) = Adodc1.Recordset("酸3开度")
               xlSheet.Cells(a, b + 11) = Adodc1.Recordset("酸3瞬时流量")
                xlSheet.Cells(a, b + 12) = Adodc1.Recordset("酸3累计流量")
                 xlSheet.Cells(a, b + 13) = Adodc1.Recordset("酸4开度")
                  xlSheet.Cells(a, b + 14) = Adodc1.Recordset("酸4瞬时流量")
                   xlSheet.Cells(a, b + 15) = Adodc1.Recordset("酸4累计流量")
                    xlSheet.Cells(a, b + 16) = Adodc1.Recordset("酸5开度")
                     xlSheet.Cells(a, b + 17) = Adodc1.Recordset("酸5瞬时流量")
                      xlSheet.Cells(a, b + 18) = Adodc1.Recordset("酸5累计流量")
                      Else
                      Exit For
                      End If
                      Adodc1.Recordset.Move 1
                      Next
    rs.MoveFirst
    xlSheet.Cells(2, 2).Value = Trim(Text1.Text) & Trim(Text2.Text)
    For irow = 0 To irowcount - 1
    Set RsUserTemp = New Recordset
    RsUserTemp.CursorLocation = adUseClient
    RsUserTemp.Open "select * from 状态数据 " _
    & "where user0_id=" & rs!user0_id, Cn, adOpenStatic, adLockReadOnly
    xlSheet.Cells(irow + 4, 1).Value = count + 1
    xlSheet.Cells(irow + 4, 2).Value = RsUserTemp!user0_id
    xlSheet.Cells(irow + 4, 3).Value = RsUserTemp!user0_name
    xlSheet.Cells(irow + 4, 4).Value = RsUserTemp!Address
    xlSheet.Cells(irow + 4, 5).Value = RsUserTemp!callno1
    Set RsUserTemp = Nothing
    Set RsOrderTemp = New Recordset
    RsOrderTemp.CursorLocation = adUseClient
    If RsOrderTemp.RecordCount = 0 Then
    ElseRsOrderTemp.MoveFirst
    Do While (Not RsOrderTemp.EOF)
    key = 0
    key = Val(Mid(str(RsOrderTemp!Order_Time), 6, 2))
    Select Case key
    Case 0
    Exit Do
    Case 1
    xlSheet.Cells(irow + 4, 6).Value = RsOrderTemp!Order_Amount
    Case 2
    xlSheet.Cells(irow + 4, 7).Value = RsOrderTemp!Order_Amount
    Case 3
    xlSheet.Cells(irow + 4, 8).Value = RsOrderTemp!Order_Amount
    Case 4
    xlSheet.Cells(irow + 4, 9).Value = RsOrderTemp!Order_Amount
    Case 5
    xlSheet.Cells(irow + 4, 10).Value = RsOrderTemp!Order_Amount
    Case 6
    xlSheet.Cells(irow + 4, 11).Value = RsOrderTemp!Order_Amount
    Case 7
    xlSheet.Cells(irow + 4, 12).Value = RsOrderTemp!Order_Amount
    Case 8
    xlSheet.Cells(irow + 4, 13).Value = RsOrderTemp!Order_Amount
    Case 9
    xlSheet.Cells(irow + 4, 14).Value = RsOrderTemp!Order_Amount
    Case 10
    xlSheet.Cells(irow + 4, 15).Value = RsOrderTemp!Order_Amount
    Case 11
    xlSheet.Cells(irow + 4, 16).Value = RsOrderTemp!Order_Amount
    Case 12
    xlSheet.Cells(irow + 4, 17).Value = RsOrderTemp!Order_Amount
    End Select
    RsOrderTemp.MoveNext
    Loop
    End If
    Set RsOrderTemp = Nothing
    count = count + 1
    rs.MoveNext
    If bl Then '因为第一条记录还未导出所以让指针回滚;
    rs.MovePrevious
    End If
    Next
    xlApp.Visible = True
    xlBook.Save
    Set xlApp = Nothing