Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : How to Call NEWID Function in a User-Defined Function
SQL Server Helper
Home > > Tip of the Day
SQL Server Helper - Tip of the Day

To read an Excel 2007 or Excel 2010 file using OPENROWSET, run the following query:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;',
'SELECT * FROM [Sheet1$]')

If the Excel 2007 or Excel 2010 file does not contain any headers, simply add the “HDR=No” to the OPENROWSET command as follows:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;HDR=No',
'SELECT * FROM [Sheet1$]')

If one of the columns in the Excel 2007 or Excel 2010 file contains a mixed type of data, where some rows may contain just numbers while others may contain alphanumeric values, add the “IMEX=1” to the second parameter of the OPENROWSET command, as follows:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;IMEX=1',
'SELECT * FROM [Sheet1$]')

To load the Excel 2007 or Excel 2010 data to a new SQL Server table, simply add the INTO clause of the SELECT statement as follows:

SELECT * 
INTO [dbo].[MyExcelData]
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;',
'SELECT * FROM [Sheet1$]')

Back to Tip of the Day List Next Tip