1. 程式人生 > >Excel資料 SQL SERVER

Excel資料 SQL SERVER

SQL code SQL SERVER 和EXCEL的資料匯入匯出 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')