如何用SQL語句查詢Excel資料?

來源:互聯網
上載者:User
如何用SQL語句查詢Excel資料?

Q:如何用SQL語句查詢Excel資料?
A:下列語句可在SQL SERVER中查詢Excel工作表中的資料。

2007和2010版本:

 SELECT *
 FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0',
 'Data Source="c:\book1.xlsx";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$]

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

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

2003版本:

SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')...[Sheet1$]

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\book1.xls', 'SELECT * FROM [Sheet1$]')

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\book1.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;

執行查詢後,再用相反順序的語句,關閉'Ad Hoc Distributed Queries'開關。

 

exec sp_configure 'Ad Hoc Distributed Queries',0;
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:\test1.xls";User ID=Admin;Password=;Extended properties="Excel 12.0 Xml;HDR=YES;IMEX=1";')...[Sheet1$]

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.