您的位置:首页 > 数据库

t-sql read xlsx

2013-07-09 10:29 127 查看
How to Read and Load an Excel 2007 or Excel 2010 File Without Using Import/Export Utility
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$]')

we must set adhocremotequeriesenabled = true :
right click the database and choose facets the link is :
http://www.mssqltips.com/sqlservertip/1673/where-is-the-surface-area-configuration-tool-in-sql-server-2008/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: