Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import an Excel file into SQL Server? [closed]

I have data in an Excel file - actually XLSX format since it is now 2020. My requirement is to get this data into SQL Server as follows:

  1. ad hoc, the use case being feeding tables with test data, or infrequent data loads of small amounts of data (say < 3k rows), and

  2. In a repeatable, robust, and possibly automated way for a production system.

like image 219
Spyder Avatar asked Sep 21 '16 07:09

Spyder


People also ask

Can I use Excel as front end for SQL Server?

You can certainly get the data you currently have in one or more Excel spreadsheets into Microsoft SQL Server. If you want to do it, the first thing you should ask yourself is whether the import is a one-off thing or if you will need to repeat this import procedure from Excel to SQL.


2 Answers

There are many articles about writing code to import an Excel file, but this is a manual/shortcut version:

If you don't need to import your Excel file programmatically using code, you can do it very quickly using the menu in SQL Server Management Studio (SSMS).

The quickest way to get your Excel file into SQL is by using the import wizard:

  1. Open SSMS (SQL Server Management Studio) and connect to the database where you want to import your file into.

  2. Import Data: in SSMS in Object Explorer under 'Databases', right-click the destination database, and select Tasks, Import Data. An import wizard will pop up (you can usually just click Next on the first screen).

    Enter image description here

  3. The next window is 'Choose a Data Source'. Select Excel:

  • In the 'Data Source' dropdown list, select Microsoft Excel (this option should appear automatically if you have Excel installed).

  • Click the 'Browse' button to select the path to the Excel file you want to import.

  • Select the version of the Excel file (97-2003 is usually fine for files with a .XLS extension, or use 2007 for newer files with a .XLSX extension)

  • Tick the 'First Row has headers' checkbox if your Excel file contains headers.

  • Click Next.

    Enter image description here

  1. On the 'Choose a Destination' screen, select destination database:
  • Select the 'Server name', Authentication (typically your sql username & password) and select a Database as destination. Click Next.

    enter image description here

  1. On the 'Specify Table Copy or Query' window:
  • For simplicity just select 'Copy data from one or more tables or views', click Next.
  1. 'Select Source Tables:' choose the worksheet(s) from your Excel file and specify a destination table for each worksheet. If you don't have a table yet the wizard will very kindly create a new table that matches all the columns from your spreadsheet. Click Next.

    Enter image description here

  2. Click Finish.

like image 78
Spyder Avatar answered Oct 25 '22 19:10

Spyder


You can also use OPENROWSET to import an Excel file in SQL Server.

SELECT * INTO Your_Table FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',                         'Excel 12.0;Database=C:\temp\MySpreadsheet.xlsx',                         'SELECT * FROM [Data$]') 
like image 37
Prafulla Avatar answered Oct 25 '22 18:10

Prafulla