Error while importing Excel file into SQL Server database

6ojccjat  于 2023-03-07  发布在  SQL Server
关注(0)|答案(1)|浏览(172)

I am trying to import an Excel file into a SQL Server database in Microsoft SQL Server Management Studio.

When I click the SQL Server Import and Export wizard server import and export wizard

this error happens:

Do you know what can lead to that?

I looked for this error in Microsoft website and other places and I saw that I need to change the authentication to - Windows authentication in the SQL Server connection.

I did it but the authentication was already set to Windows authentication

piok6c0g

piok6c0g1#

Excel can have all kinds of weird formatting that throws things off during the import process. You should save your Excel file as a CSV file, and import the CSV.

bulk insert yourtable from 'C:\file.csv'
with ( FIELDTERMINATOR =',', FIRSTROW = 2 )

Or, if you are open to a VBA solution to move data from your Excel file to your SQL Server database, run the code below.

Sub testexportsql()
    Dim Cn As ADODB.Connection
    Dim ServerName As String
    Dim DatabaseName As String
    Dim TableName As String
    Dim UserID As String
    Dim Password As String
    Dim rs As ADODB.Recordset
    Dim RowCounter As Long

    Dim NoOfFields As Integer
    Dim StartRow As Long
    Dim EndRow As Long

    Dim ColCounter As Integer

    Set rs = New ADODB.Recordset

    ServerName = "server_name" ' Enter your server name here
    DatabaseName = "db_name" ' Enter your  database name here
    TableName = "table_name" ' Enter your Table name here
    UserID = "" ' Enter your user ID here
     ' (Leave ID and Password blank if using windows Authentification")
    Password = "" ' Enter your password here
    NoOfFields = 10 ' Enter number of fields to update (eg. columns in your worksheet)
    StartRow = 2 ' Enter row in sheet to start reading  records
    EndRow = 100 ' Enter row of last record in sheet

     '  CHANGES
    Dim shtSheetToWork As Worksheet
    Set shtSheetToWork = ActiveWorkbook.Worksheets("sheet_name")
     '********

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
    ";Uid=" & UserID & ";Pwd=" & Password & ";"

    rs.Open TableName, Cn, adOpenKeyset, adLockOptimistic

     'EndRow = shtSheetToWork.Cells(Rows.Count, 1).End(xlUp).Row
    For RowCounter = StartRow To EndRow
        rs.AddNew
        For ColCounter = 1 To NoOfFields
            rs(ColCounter - 1) = shtSheetToWork.Cells(RowCounter, ColCounter)
        Next ColCounter
        Debug.Print RowCounter
    Next RowCounter
    rs.UpdateBatch

     ' Tidy up
    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing

End Sub

Or, simply run the code below in SQL Server.

Export data from Excel to existing SQL Server table (edited)
Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=C:\testing.xls;HDR=YES', 
    'SELECT * FROM [SheetName$]')

相关问题