注册 登录
编程论坛 SQL Server论坛

急!求助excel怎样导入sql中(新手学习中),谢谢。

mummy365 发布于 2011-03-27 12:20, 1074 次点击
我已经启动了
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

exec sp_configure 'show advanced options',0
reconfigure

之后我运行(我用的SQL是2008,excel是2003)
insert into jr select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 8.0;HDR=YES;DATABASE=f:jr.xls',sheet1$)

提示错误:
链接服务器"(null)"的 OLE DB 访问接口 "MICROSOFT.JET.OLEDB.4.0" 返回了消息 "未指定的错误"。
消息 7303,级别 16,状态 1,第 1 行
无法初始化链接服务器 "(null)" 的 OLE DB 访问接口 "MICROSOFT.JET.OLEDB.4.0" 的数据源对象。

请问这个我怎么才能正确的导入呢?谢谢,急!!
2 回复
#2
qingshuiliu2011-03-27 17:39
一段用过的导入SQL:
INSERT INTO OPENROWSET ('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=c:\test.xls;Extended Properties=Excel 12.0 XML; HDR=YES;IMEX=0;',
'SELECT * FROM [Sheet1$]')
GO
c:\test.xls是excel路径,Sheet1是excel中的工作薄
#3
stephenchern2011-03-29 09:17
问对人了 这个我之前遇到过
下面是代码,其备注都在里面

/*
导入字段溢出:
修改注册表:HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel
下双击右边的"TypeGuessRows"选项,将"数值数据"改成0。 修改之后关机重新启动。
原因是:将Excel表的数据导入数据库的时候,Jet引擎根据"TypeGuessRows"选项的值所代表的行数判断内容的数据类型,默认是根据前8行的内容判断数据类型,修改成0后,它会对每行的内容进行判断,不过这样做会影响效率。
原因是驱动默认情况下根据列的前8行数据判定列长度,修改为0后需要检查整列数据来判定列长度,超过255个字符默认为ntext格式,少于255个字符则默认为nvarchar(255)。
*/
if exists (select 1 from sysobjects where name = 'P_Import' and type = 'P') drop procedure P_Import
go
create proc P_Import
    @FileName            nvarchar(255),
    @SheetName            nvarchar(255),
    @ImportTableName    nvarchar(255)
as
    declare @SqlStr varchar(8000)
    set @SqlStr = 'if exists (select 1 from sysobjects where name = '''+@ImportTableName+''' and type = ''U'') drop table '+@ImportTableName+'; '
                + 'select * into '+@ImportTableName+' from '
                + 'openrowset(''MICROSOFT.JET.OLEDB.4.0'', ''Excel 5.0; HDR=YES; DATABASE='+@FileName+''', '+@SheetName+') '
    exec (@SqlStr)
go
/*
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

exec P_Import 'D:\Project\EGMIS\Import\1.1.0、NGUSER-Operate.xls', 'ds1$', 'T_Import_Operate'

exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
*/



--导入xls文件
/*
导入字段溢出:
修改注册表:HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel
下双击右边的"TypeGuessRows"选项,将"数值数据"改成0。 修改之后关机重新启动。
原因是:将Excel表的数据导入数据库的时候,Jet引擎根据"TypeGuessRows"选项的值所代表的行数判断内容的数据类型,默认是根据前8行的内容判断数据类型,修改成0后,它会对每行的内容进行判断,不过这样做会影响效率。
原因是驱动默认情况下根据列的前8行数据判定列长度,修改为0后需要检查整列数据来判定列长度,超过255个字符默认为ntext格式,少于255个字符则默认为nvarchar(255)。
*/

--启用Ad Hoc Distributed Queries
set nocount on
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

begin try
begin tran
    exec P_Import 'D:\Import\1.1.0、NGUSER-Operate.xls', 'ds1$', 'T_Import_Operate'
commit tran
end try
begin catch
    if @@trancount > 0 rollback tran
    raiserror('事务运行错误',16,2)
    exec P_ErrorLog
end catch

--使用完成后,关闭Ad Hoc Distributed Queries
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
1