SQL Server數(shù)據(jù)庫與EXCEL數(shù)據(jù)導(dǎo)入與導(dǎo)出
以下的文章主要描述的是SQL Server數(shù)據(jù)庫與EXCEL數(shù)據(jù)庫的數(shù)據(jù)導(dǎo)入與導(dǎo)出的實(shí)際操作步驟的描述,如果你對SQL Server數(shù)據(jù)庫與EXCEL數(shù)據(jù)庫的數(shù)據(jù)導(dǎo)入與導(dǎo)出的實(shí)際操作步驟心存好奇的話,以下的文章將會揭開它的神秘面紗。
1、在SQL Server里查詢Excel數(shù)據(jù):
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的數(shù)據(jù)導(dǎo)入SQL Server數(shù)據(jù)庫 :
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 $]
實(shí)例: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中查詢到的數(shù)據(jù)導(dǎo)成一個Excel文件T-SQL代碼:
EXEC master……xp_cmdshell \\\\\\\'bcp 庫名。dbo.表名out c:\\\\\\\\Temp.xls -c -q -S"servername" -U"sa" -P""\\\\\\\'參數(shù):S 是SQL服務(wù)器名;U是用戶;P是密碼說明:還可以導(dǎo)出文本文件等多種格式
實(shí)例: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中應(yīng)用ADO導(dǎo)出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插入數(shù)據(jù):
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\\\\\\\')
以上的相關(guān)內(nèi)容就是對SQL Server數(shù)據(jù)庫 和EXCEL的數(shù)據(jù)導(dǎo)入導(dǎo)出的介紹,望你能有所收獲。
【編輯推薦】