欢迎光临
我们一直在努力

sql server 和excel的数据导入导出_数据库技巧

建站超值云服务器,限时71元/月

1、在SQL SERVER里查询Excel数据:


SELECT *
FROM OpenDataSource( Microsoft.Jet.OLEDB.4.0,
  Data Source=”c:\department.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…Sheet1$



SELECT * into newtable
FROM OpenDataSource( Microsoft.Jet.OLEDB.4.0,
 Data Source=”c:\department.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…[Sheet1$]



SELECT * FROM dbo.newtable


SELECT * FROM dbo.department


SELECT w,w2
FROM OpenDataSource( Microsoft.Jet.OLEDB.4.0,
  Data Source=”c:\book1.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…Sheet1$



SELECT w4,w3 into newtable2
FROM OpenDataSource( Microsoft.Jet.OLEDB.4.0,
 Data Source=”c:\book1.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…[Sheet1$]


SELECT * FROM dbo.newtable2


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文件


EXEC master..xp_cmdshell
bcp testexcel.dbo.newtable
out
c:\book8.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
*/
EXEC master..xp_cmdshell
bcp “SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname”
queryout
C:\ book9.xls -c -Sservername -Usa -Psa


  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)


 insert into OpenDataSource( Microsoft.Jet.OLEDB.4.0,
Data Source=”c:\book3.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…Sheet1$
(A1,A2,A3) values (1,2,3)



INSERT INTO OPENDATASOURCE( Microsoft.JET.OLEDB.4.0,
Extended Properties=Excel 8.0;Data source=”c:\book3.xls”)…Sheet1$
( A1, A2) VALUES (20, Test)


SELECT *
FROM OpenDataSource( Microsoft.Jet.OLEDB.4.0,
  Data Source=”c:\book3.xls”;User ID=Admin;Password=;Extended properties=Excel 5.0)…Sheet1$


 总结:利用以上语句,我们可以方便地将SQL SERVER、ACCESS和EXCEL电子表格软件中的数据进行转换,为我们提供了极大方便!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » sql server 和excel的数据导入导出_数据库技巧
分享到: 更多 (0)