Q:如何用SQL语句查询Excel数据?
A:下列语句可在SQL SERVER中查询Excel工作表中的数据。
2007和2010版本:
SELECT *
FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0',
'Data Source="c:ook1.xlsx";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$]
FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0',
'Data Source="c:ook1.xlsx";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$]
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:ook1.xlsx', 'SELECT * FROM [Sheet1$A1:D100]')
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:ook1.xlsx', 'SELECT * FROM [Sheet1$]')
2003版本:
SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:ook1.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')...[Sheet1$]
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:ook1.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')...[Sheet1$]
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:ook1.xls', 'SELECT * FROM [Sheet1$]')
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:ook1.xls', 'SELECT * FROM [Sheet1$A1:D100]')
而且,还需要用拥有服务器sysadmin角色权限的登录帐号,先执行下列语句打开SQL SERVER的'Ad Hoc Distributed Queries'开关。
exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;
执行查询后,再用相反顺序的语句,关闭'Ad Hoc Distributed Queries'开关。
exec sp_configure 'Ad Hoc Distributed Queries',0;
reconfigure;exec sp_configure 'show advanced options',0;reconfigure;
reconfigure;exec sp_configure 'show advanced options',0;reconfigure;
# 网站名称: Excel技巧网|Excel 2010/2007新领地
# 署名作者: apolloh
# 版权声明: 版权所有 非作者本人转载须经许可并注明作者和出处
# 本文来自:
# 引用作品:
# 适用版本: 2010 2007 2003以前版本
# 语言环境: 简体中文 (頁麵右上角簡繁轉換)
# 内容简介:
# 文章地址: http://www.exceltip.net/thread-5862-1-1-0.html
# 学习方法: 掌握Excel技巧的关键是动手操作|下载≠知识
2007和2003都可用
SELECT * FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0','Data Source="D: est1.xls";User ID=Admin;Password=;Extended properties="Excel 12.0 Xml;HDR=YES;IMEX=1";')...[Sheet1$]