熱線電話:13121318867

登錄
首頁精彩閱讀SQL SERVER 和EXCEL的數據導入導出
SQL SERVER 和EXCEL的數據導入導出
2017-12-29
收藏

SQL SERVER 和EXCEL的數據導入導出

sql下操作excel需要在使用前運行啟用代碼,使用結束后執行關閉代碼

--啟用

--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',0reconfigure

1、在SQL SERVER里查詢Excel數據: 
-- ====================================================== 
SELECT * 
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 
'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$] 
下面是個查詢的示例,它通過用于 Jet 的 OLE DB 提供程序查詢 Excel 電子表格。 
SELECT * 
FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0', 
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions 
-------------------------------------------------------------------------------------------------
2、將Excel的數據導入SQL server : 
-- ====================================================== 
SELECT * into newtable 
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 
'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$] 
實例: 
SELECT * into newtable 
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions 
-------------------------------------------------------------------------------------------------
3、將SQL SERVER中查詢到的數據導成一個Excel文件 
-- ====================================================== 
T-SQL代碼: 
EXEC master..xp_cmdshell 'bcp 庫名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""' 
參數:S 是SQL服務器名;U是用戶;P是密碼 
說明:還可以導出文本文件等多種格式 
實例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"' 
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword' 
在VB6中應用ADO導出EXCEL文件代碼: 
Dim cn  As New ADODB.Connection 
cn.open "Driver={SQL Server};Server=WEBSVR;DataBase=WebMis;UID=sa;WD=123;" 
cn.execute "master..xp_cmdshell 'bcp "SELECT col1, col2 FROM 庫名.dbo.表名" queryout E:\DT.xls -c -Sservername -Usa -Ppassword'" 
------------------------------------------------------------------------------------------------ 

4、在SQL SERVER里往Excel插入數據: 
-- ====================================================== 
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 
'Data Source="c:\Temp.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...table1 (A1,A2,A3) values (1,2,3) 

T-SQL代碼: 
INSERT INTO  
OPENDATASOURCE('Microsoft.JET.OLEDB.4.0',  
'Extended Properties=Excel 8.0;Data source=C:\training\inventur.xls')...[Filiale1$]  
(bestand, produkt) VALUES (20, 'Test')  
-------------------------------------------------------------------------------------------------
總結:利用以上語句,我們可以方便地將SQL SERVER、ACCESS和EXCEL電子表格軟件中的數據進行轉換,為我們提供了極大方便! 
EXEC  master..xp_cmdshell  'bcp  "select OrderID,ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry from Northwind.dbo.orders"  queryout  "d:\Oreders.txt" -t"|"    -c  -q  -S"127.0.0.1"  -U"sa"  -P""'  
SELECT  *    
FROM  OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data  Source="xxxx.xls";Extended  Properties="Excel  8.0";IMEX=1;Persist  Security  Info=False')...[a1$]


SQL

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢