9

Can anyone advise how to bulk insert from .xlsx file?

I tried the below query already:

BULK INSERT #EVB FROM 'C:\Users\summer\Desktop\Sample\premise.xlsx' 
WITH (FIELDTERMINATOR = '\t', ROWTERMINATOR = '\n', FIRSTROW = 2);

SELECT * FROM #EVB

I also tried with FIELDTERMINATOR like "**\t**", "**,**", "**;**", "**|**", but this doesn't work either.

Unfortunately, there is no error message.

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
SƲmmēr Aƥ
  • 2,374
  • 6
  • 24
  • 29

4 Answers4

28

you can save the xlsx file as a tab-delimited text file and do

BULK INSERT TableName
        FROM 'C:\SomeDirectory\my table.txt'
            WITH
    (
                FIELDTERMINATOR = '\t',
                ROWTERMINATOR = '\n'
    )
GO
wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197
  • 2
    This was extremely helpful. The row terminators in the actual txt files that Excel exports are CRLF terminated. I tried RowTerminator = "\r\n" and repeatedly could not get it to import. Using \n solved the problem. I know this is is an old recommendation, but THANK YOU! It's still current. – EricI Mar 14 '20 at 18:32
  • This is the method I use but the file size is so large, it's very time consuming to open a 150mb xlsx file and save as text. Not to mention, the random bugs. Is there a native way to bulk insert directly from XLSX? – PowerUser Aug 09 '22 at 19:56
3

You need to use OPENROWSET

Check this question: import-excel-spreadsheet-columns-into-sql-server-database

Community
  • 1
  • 1
Turque
  • 708
  • 7
  • 18
1

Create a linked server to your document

http://www.excel-sql-server.com/excel-import-to-sql-server-using-linked-servers.htm

Then use ordinary INSERT or SELECT INTO. If you want to get fancy, you can use ADO.NET's SqlBulkCopy, which takes just about any data source that you can get a DataReader from and is pretty quick on insert, although the reading of the data won't be esp fast.

You could also take the time to transform an excel spreadsheet into a text delimited file or other bcp supported format and then use BCP.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
0

It can be done using SQL Server Import and Export Wizard. But if you're familiar with SSIS and don't want to run the SQL Server Import and Export Wizard, create an SSIS package that uses the Excel Source and the SQL Server Destination in the data flow.