使用VB6从sql 2008中提取数据到excel

时间:2015-11-09 18:23:43

标签: sql excel vb6

好的,我会粘贴下面的代码。我正在尝试在sql server 2008中循环遍历表,但是当excel打开时,只显示表中第一行的数据。如何扩展循环以显示表中的所有记录。记录计数是13,但我只看到第一行(名字,姓氏)的数据f

Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim X As Integer
Dim y As Integer
Dim strReportFileName As String
Dim strTmpMsg As String
Dim intResponse As Integer
Dim xlRow As Integer

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add

xlSheet.Name = "Test Extract"

With xlSheet.PageSetup
    .Orientation = xlLandscape
End With
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
strSQL = "SELECT FirstName, LastName from dbo.tblTest_clients"
rs.Open strSQL, g_cnDatabase
Debug.Print strSQL
MsgBox rs.RecordCount

rs.MoveFirst

For y = 0 To rs.Fields.Count - 1
    Select Case y
        Case 0
            With xlSheet.Cells(xlRow, y + 1)
                If Not IsNull(rs("FirstName").Value) Then
                    .Value = rs("FirstName")
                End If
            End With
        Case 1
            With xlSheet.Cells(xlRow, y + 1)
                If Not IsNull(rs("LastName").Value) Then
                    .Value = rs("LastName")
                End If
            End With
        Case Else
            'do nothing
    End Select
Next y

If xlApp.Version = "10.0" Then
    strReportFileName = g_strReportWriteDir & "\ClientInfo" & Format(Now, "YYYYMMDDHHNNSS") & ".xls "
    xlBook.SaveAs strReportFileName
Else
    strReportFileName = g_strReportWriteDir & "\ClientInfo" & Format(Now, "YYYYMMDDHHNNSS") & ".xls"
    xlBook.SaveAs strReportFileName, 56
End If

xlBook.Close
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
'xlApp.Quit

strTmpMsg = "Client Info Extract File: " & strReportFileName & " was created successfully."
intResponse = MsgBox(strTmpMsg & vbCrLf & vbCrLf & "Do you wish to View?" & vbCrLf & vbCrLf & "Viewing the Extract opens another program windo -please be sure to CLOSE it once you are done reviewing the extract.", vbYesNo, "Client Extract View Confirmation.....")
    If intResponse = vbYes Then
        If ShellEx(strReportFileName, essSW_SHOWMAXIMIZED, , , "Open", frmReports.hWnd) Then
        End If
    End If

2 个答案:

答案 0 :(得分:2)

rs.MoveFirst

With xlSheet.Cells(xlRow)
    .Cells(1).Value = "First Name"
    .Cells(2).Value = "Last Name"
End With
xlRow = xlRow + 1

Do While Not rs.EOF
    With xlSheet.Cells(xlRow)
        If Not IsNull(rs("FirstName").Value) Then .Cells(1).Value = rs("FirstName")
        If Not IsNull(rs("LastName").Value) Then .Cells(2).Value = rs("LastName")
    End With
    rs.movenext
    xlRow = xlRow + 1
Loop

答案 1 :(得分:0)

  1. 你永远不会将xlRow设置为任何值或迭代它,因此代码将继续写入该行 - 因此,为什么你只看到你的最后一个值。
  2. 你可以用更干净的代码做你想做的事。
  3. 见下文

    rs.MoveFirst
    
    xlRow = 1
    
    Do Until rs.EOF
    
       'puts first name in column A
       If Not IsNull(rs("FirstName")) Then xlSheet.Cells(xlRow, 1).Value = rs("FirstName")
       'puts last name in column B
       If Not IsNull(rs("LastName")) Then xlSheet.Cells(xlRow, 2).Value = rs("LastName")
    
        rs.MoveNext
        xlRow = xlRow + 1
    
    Loop