这佷简单啊:<br><br>SQL SERVER 与ACCESS、EXCEL的数据转换 <br><br>熟悉SQL SERVER 2000的数据库管理员都知道,其DTS可以进行数据的导入导出,其实,我们也可以使用Transact-SQL语句进行导入导出操作。在Transact-SQL语句中,我们主要使用OpenDataSource函数、OPENROWSET 函数,关于函数的详细说明,请参考SQL联机帮助。利用下述方法,可以十分容易地实现SQL SERVER、ACCESS、EXCEL数据转换,详细说明如下:<br><br> <br><br>一、 SQL SERVER 和ACCESS的数据导入导出<br><br>Transact-SQL语句进行导入导出:<br><br>1. 在SQL SERVER里查询access数据:<br><br>-- ======================================================<br><br>SELECT * <br><br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br>'Data Source="c:/DB.mdb";User ID=Admin;Password=')...表名<br><br>-------------------------------------------------------------------------------------------------<br><br> <br><br>2. 将access导入SQL server <br><br>-- ======================================================<br><br>在SQL SERVER 里运行:<br><br>SELECT *<br><br>INTO newtable<br><br>FROM OPENDATASOURCE ('Microsoft.Jet.OLEDB.4.0', <br><br> 'Data Source="c:/DB.mdb";User ID=Admin;Password=' 
...表名<br><br>-------------------------------------------------------------------------------------------------<br><br> <br><br>3. 将SQL SERVER表里的数据插入到Access表中<br><br>-- ======================================================<br><br>在SQL SERVER 里运行:<br><br>insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br> 'Data Source=" c:/DB.mdb";User ID=Admin;Password=')...表名 <br><br>(列名1,列名2)<br><br>select 列名1,列名2 from sql表<br><br> <br><br>实例:<br><br>insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', <br><br> 'C:/db.mdb';'admin';'', Test) <br><br>select id,name from Test<br><br> <br><br> <br><br>INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'c:/trade.mdb'; 'admin'; '', 表名)<br><br>SELECT *<br><br>FROM sqltablename<br><br>-------------------------------------------------------------------------------------------------<br><br> <br><br> <br><br> <br><br> <br><br> <br><br>二、 SQL SERVER 和EXCEL的数据导入导出<br><br> <br><br>1、在SQL SERVER里查询Excel数据:<br><br>-- ======================================================<br><br>SELECT * <br><br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br>'Data Source="c:/book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]<br><br> <br><br>下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。<br><br>SELECT * <br>FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0',<br> 'Data Source="c:/Finance/account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br>-------------------------------------------------------------------------------------------------<br><br> <br><br>2、将Excel的数据导入SQL server :<br><br>-- ======================================================<br><br>SELECT * into newtable<br><br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br> 'Data Source="c:/book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]<br><br> <br><br>实例:<br><br>SELECT * into newtable<br><br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br> 'Data Source="c:/Finance/account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br>-------------------------------------------------------------------------------------------------<br><br> <br><br>3、将SQL SERVER中查询到的数据导成一个Excel文件<br><br>-- ======================================================<br><br>T-SQL代码:<br><br>EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:/Temp.xls -c -q -S"servername" -U"sa" -P""'<br><br>参数:S 是SQL服务器名;U是用户;P是密码<br><br>说明:还可以导出文本文件等多种格式<br><br> <br><br>实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:/temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'<br><br> <br><br> 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'<br><br> <br><br>在VB6中应用ADO导出EXCEL文件代码: <br><br>Dim cn As New ADODB.Connection<br><br>cn.open "Driver={SQL Server};Server=WEBSVR;DataBase=WebMis;UID=sa;WD=123;"<br><br>cn.execute "master..xp_cmdshell 'bcp "SELECT col1, col2 FROM 库名.dbo.表名" queryout E:/DT.xls -c -Sservername -Usa -Ppassword'"<br><br>-------------------------------------------------------------------------------------------------<br><br> <br><br>4、在SQL SERVER里往Excel插入数据:<br><br>-- ======================================================<br><br>insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br><br>'Data Source="c:/Temp.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...table1 (A1,A2,A3) values (1,2,3)<br><br> <br><br>T-SQL代码:<br><br>INSERT INTO <br><br> OPENDATASOURCE('Microsoft.JET.OLEDB.4.0', <br><br> 'Extended Properties=Excel 8.0;Data source=C:/training/inventur.xls')...[Filiale1$] <br><br> (bestand, produkt) VALUES (20, 'Test')