2010-12-06 151 views
0

我试着执行这个存储过程,意思是将已经创建的Excel表格的格式复制到另一个Excel表格中;前者充当模板。然后,存储过程将使用来自SQL查询的结果集来填充新的Excel表。从SQL Server 2005导入查询的结果集到MS Excel中

执行时,它提供了以下错误:

Insert ExcelSource...[ExcelTable$] (A,B,C) select convert(varchar(200),USER_ID), FIRST_NAME, 
Convert (varchar(20),CREATEDTIME) 
from SERV..AaUser 
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource" returned message "Cannot start your application. The workgroup information file is missing or opened exclusively by another user.". 
Msg 7399, Level 16, State 1, Line 1 
The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource" reported an error. Authentication failed. 
Msg 7303, Level 16, State 1, Line 1 
Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource". 

存储过程的语法是:

Create proc sp_write2Excel (@fileName varchar(100), 

            @NumOfColumns tinyint, 

            @query  varchar(200)) 

as 

begin 

     declare @dosStmt varchar(200) 

     declare @tsqlStmt varchar(500) 

     declare @colList varchar(200) 

     declare @charInd tinyint 



     set nocount on 



     -- construct the columnList A,B,C ... 

     -- until Num Of columns is reached. 



     set @charInd=0 

     set @colList = 'A' 

     while @charInd < @NumOfColumns - 1 

     begin 

      set @charInd = @charInd + 1 

      set @colList = @colList + ',' + char(65 + @charInd) 

     end 



     -- Create an Empty Excel file as the target file name by copying the template Empty excel File 

     set @dosStmt = ' copy C:\emp\empty.xls ' + @fileName 

     exec master..xp_cmdshell @dosStmt 



     -- Create a "temporary" linked server to that file in order to "Export" Data 

     EXEC sp_addlinkedserver 'ExcelSource', 

     'Jet 4.0', 

     'Microsoft.Jet.OLEDB.4.0', 

     @fileName, 

     NULL, 

     'Excel 5.0' 



     -- construct a T-SQL statement that will actually export the query results 

     -- to the Table in the target linked server 

     set @tsqlStmt = 'Insert ExcelSource...[ExcelTable$] ' + ' (' + @colList + ') '+ @query 



     print @tsqlStmt 



     -- execute dynamically the TSQL statement 

     exec (@tsqlStmt) 



     -- drop the linked server 

     EXEC sp_dropserver 'ExcelSource' 

     set nocount off 

end 

非常感谢您的受众和预期的帮助。

干杯, Tunde

回答

0

在同一台计算机上安装Excel作为你的SQL Server实例? JET司机办公室有可能失踪。

编辑:

我认为我误读了邮政 - 这听起来像文件被打开了。 Excel文件一次只能由一个用户打开,SQL Server需要独占访问此文件。使用LockHunter可能有助于确定将文件绑定到什么位置。

+0

是的,它们都安装在同一台机器上。谢谢,我在SQl sERVER和机器上的管理员身份登录为sa。 – Tunde 2010-12-07 15:05:23