2010-12-06 6 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

엑셀은 SQL Server 인스턴스와 동일한 시스템에 설치되어 있습니까? JET 드라이버의 Office가 누락되었을 가능성이 있습니다.

편집 : - 파일이 이미 열려있는 것 같은데

내가 게시물을 오해 생각합니다. Excel 파일은 한 번에 한 사용자 만 열 수 있으며 SQL Server는이 파일에 단독으로 액세스해야합니다. LockHunter을 사용하면 파일을 묶는 내용을 결정하는 데 도움이 될 수 있습니다.

+0

예 모두 동일한 컴퓨터에 설치됩니다. 고마워요. 그리고 SQl sERVER와 관리자 컴퓨터에 sa로 로그인했습니다. – Tunde

관련 문제