积极答复者
无法用Select into 语句从SQL导出到EXCEL

问题
-
各位好。我使用C#执行cmd.ExecuteNonQuery();
SELECT * INTO [Excel 8.0;Database=D:\Source_Codes\SMD_ERP_SYSTEM\SMD_ARM\SMD_ARM\bin\Debug\book1.xls;].[Sheet1] FROM gy_Customer
这个语句的时候,提示失败,原因:
The specified schema name "Excel 8.0;Database=D:\Source_Codes\SMD_ERP_SYSTEM\SMD_ARM\SMD_ARM\bin\Debug\book1.xls;" either does not exist or you do not have permission to use it.
请问如何解决这个。。?我从MSDN上看到这个语句是没有execl也会自动创建excel的。。
- 已移动 Sheng Jiang 蒋晟 2010年4月27日 17:45 (发件人:ADO.NET 与 LINQ)
答案
-
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$) select * from 表名
這樣用
如果是生成excel時用bcp
--导出查询的情况
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout "c:\test.xls" /c -/S"服务器名" /U"用户名" -P"密码"'
ROY WU(吳熹)- 已标记为答案 中國風MVP, Moderator 2010年4月30日 2:28
全部回复
-
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$) select * from 表名
這樣用
如果是生成excel時用bcp
--导出查询的情况
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout "c:\test.xls" /c -/S"服务器名" /U"用户名" -P"密码"'
ROY WU(吳熹)- 已标记为答案 中國風MVP, Moderator 2010年4月30日 2:28
-
declare @constr varchar(8000),@cmd varchar(8000),@obj int,@out int select @constr = 'Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES;Database=c:\Book1.xls"' select @cmd = 'create table [systables]([name] memo)' exec sp_OACreate 'adodb.connection',@obj out exec sp_OAMethod @obj,'open',null,@constr exec sp_OAMethod @obj,'execute',@out out,@cmd exec sp_OAMethod @obj,'close',null exec sp_OADestroy @obj result:成功建立数据表 c:\book1.xls, Sheet Name: systables insert into openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=c:\Book1.xls','select [name] from [systables$]') select name from sys.tables result:(17 行受影响) ---------------------- 安全性 EXEC sp_configure 'show advanced options', 1 RECONFIGURE with override EXEC sp_configure 'Ole Automation Procedures', 1 RECONFIGURE with override EXEC sp_configure 'Ad Hoc Distributed Queries', 1 RECONFIGURE with override
hello