多个excel表格如何导入到同一张表?
有50多个excel表格,里面的字段全部是一样的。我现在要把这50多个表格都导入到同一张数据库表里,该如何操作最快?
在同一张表哦。。
[解决办法]
结构都一样是吧
下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。
INSERT INTO TB SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
[解决办法]
SSIS
[解决办法]
每次都只把excel表帮sql server的一个固定的表里面导.
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')
[解决办法]
union all select *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions2
union all select *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions3
union all select *
......
union all select *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactionsN;
[解决办法]
第二次导的时候就附加资料..应该可以!
不过也要一个个的导...
[解决办法]
---------------------------------------- Author : htl258(Tony)-- Date : 2010-04-07 21:09:45-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) -- Jul 9 2008 14:43:34 -- Copyright (c) 1988-2008 Microsoft Corporation-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)-- Subject:将指定文件夹下目录相同的EXCEL文件批量导入到SQL数据库中指定的表--------------------------------------IF OBJECT_ID('Sp_InputExceltoSQL') IS NOT NULL DROP PROC Sp_InputExceltoSQLGOCREATE PROC Sp_InputExceltoSQL@dir NVARCHAR(100),--EXCEL文件存放路径:如D:\ExcelFiles@tabname NVARCHAR(50) --定义导入到数据库中的表名ASSET NOCOUNT ONDECLARE @cmd NVARCHAR(1000)CREATE TABLE #t([filename] NVARCHAR(1000))IF RIGHT(@dir,1)<>'\' SET @dir=@dir+'\'SET @cmd = N'dir "' + @dir + '*.xls" /B'INSERT #t EXEC master..xp_cmdshell @cmdDELETE #t WHERE [filename] IS NULL--在SQL中创建表:DECLARE @S nvarchar(MAX)IF OBJECT_ID(@tabname) IS NULLBEGIN SELECT TOP 1 @S='SELECT TOP 0 * INTO '+ @tabname+' FROM OPENROWSET( ''MICROSOFT.JET.OLEDB.4.0'', ''EXCEL 5.0;HDR=YES;IMEX=2;DATABASE='+@DIR+[filename]+''' ,''SELECT * FROM [Sheet1$]'')' FROM #t EXEC(@S)END--开始导入目录SET @S = ''SELECT @S = @S + 'INSERT '+@tabname+' SELECT * FROM OPENROWSET( ''MICROSOFT.JET.OLEDB.4.0'', ''EXCEL 5.0;HDR=YES;IMEX=2;DATABASE='+@DIR+FILENAME+''' ,''SELECT * FROM [Sheet1$]'')' FROM #t EXEC(@S)SET NOCOUNT OFFGOEXEC Sp_InputExceltoSQL 'D:\ExcelFiles','Tony'SELECT * FROM Tony/*a b c d---------------------- ---------------------- ---------------------- ----------------------1 2 3 45 6 7 8(2 行受影响)*/
[解决办法]
好象excel可以一次性将所有的表格汇到一张表格中,
然后在导不是更好???
[解决办法]
使用SSIS就很方便了,可以不用编写代码.
[解决办法]
http://www.bhcode.net/article/20080731/1973.html
SSIS批量导入
[解决办法]