将数据从SQL导入Excel

时间:2017-10-26 13:56:03

标签: c# sql-server excel vb.net excel-vba vba

我正在VB.net中编写应用程序,我试图将一些表从Sql server传输到Excel文件。有很多行,所以我不想使用循环。我试图改编我发现的代码:

 Dim cnPubs As ADODB.Connection
    cnPubs = New ADODB.Connection
    Dim strConn As String
    strConn = "PROVIDER= SQLOLEDB;"
    strConn = strConn & "DATA SOURCE=(LocalDB)\v11.0;"
    strConn = strConn & "AttachDbFilename='" & DBPath & "';"
    strConn = strConn & " INTEGRATED SECURITY=sspi;"
    cnPubs.Open(strConn)
    Dim rsPubs As ADODB.Recordset
    rsPubs = New ADODB.Recordset

    With rsPubs
        .ActiveConnection = cnPubs
        .Open("SELECT * FROM dbo.Table")
        ExWS.Range("A1").CopyFromRecordset(rsPubs)
        .Close()
    End With
    ExApp.Visible = True
    cnPubs.Close()
    rsPubs = Nothing
    cnPubs = Nothing

我得到的是:

  

其他信息:[DBNETLIB] [ConnectionOpen(Connect())。] SQL Server不存在或访问被拒绝。

我还想补充一点,我已经使用SqlClient.SqlConnection连接到这个数据库,我可以正常执行查询。

我看到两个解决方案:

  • 修复了ADODB.Connection的问题,但我担心它会在我更改数据库时返回
  • 使用SqlConnection找到一种复制整个表(无循环)的方法。

1 个答案:

答案 0 :(得分:0)

如果你有SSIS,你可以使用它。作为替代方案,您可以考虑以下概念。 。 。

Sub ADOExcelSQLServer()
     ' Carl SQL Server Connection
     '
     ' FOR THIS CODE TO WORK
     ' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
     '

    Dim Cn As ADODB.Connection
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim SQLStr As String
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    Server_Name = "EXCEL-PC\EXCELDEVELOPER" ' Enter your server name here
    Database_Name = "AdventureWorksLT2012" ' Enter your database name here
    User_ID = "" ' enter your user ID here
    Password = "" ' Enter your password here
    SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
    ";Uid=" & User_ID & ";Pwd=" & Password & ";"

    rs.Open SQLStr, Cn, adOpenStatic
     ' Dump to spreadsheet
    With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here
        .ClearContents
        .CopyFromRecordset rs
    End With
     '            Tidy up
    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing
End Sub

OR

Sub ADOExcelSQLServer()

    Dim Cn As ADODB.Connection
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim SQLStr As String
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    Server_Name = "LAPTOP\SQL_EXPRESS" ' Enter your server name here
    Database_Name = "Northwind" ' Enter your  database name here
    User_ID = "" ' enter your user ID here
    Password = "" ' Enter your password here
    SQLStr = "SELECT * FROM Orders" ' Enter your SQL here

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
    ";Uid=" & User_ID & ";Pwd=" & Password & ";"

    rs.Open SQLStr, Cn, adOpenStatic

    With Worksheets("Sheet1").Range("A2:Z500")
        .ClearContents
        .CopyFromRecordset rs
    End With

    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing
End Sub

ORRRRRRrrr

Sub TestMacro()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=NORTHWIND.MDF;"

'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"

'Now open the connection.
cnPubs.Open strConn

' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

With rsPubs
    ' Assign the Connection object.
    .ActiveConnection = cnPubs
    ' Extract the required records.
    .Open "SELECT * FROM Categories"
    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A1").CopyFromRecordset rsPubs

    ' Tidy up
    .Close
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing

End Sub

https://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/

另外。 。 。

https://www.red-gate.com/simple-talk/sql/t-sql-programming/sql-server-excel-workbench/

最后。 。

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction