2011-09-16 11 views
3

SQL Sever 2005를 사용하여 Excel 파일에서 시트 이름을 얻는 방법은 무엇입니까?SQL Server에서 Excel 시트 이름 가져 오기

것을 유의 사항 :

  • 더 프런트 엔드 (C 번호, VB, PHP, 등)이 없다;
  • SQL Server 2005 만 사용하여 시트 이름을 얻으려고합니다.

감사합니다.

답변

2

수 없습니다. Excel에서 데이터를 읽는 데는 두 가지 경로가 있습니다. 하나는 통합 문서의 워크 시트를 통해 열거 할 수있는 COM/OLE automation 경로입니다. TSQL이하지 않을 절차 언어가 필요합니다. CLR 방식을 혼합하여 사용할 수 있다면 BCL 목록에없는 Office 라이브러리에 액세스 할 수 있다고 생각하지 않습니다.

두 번째 경로는이 경우 열기 쿼리를 통해 Jet driver을 사용하지만 설정의 일부로 액세스 할 파일과 워크 시트를 명시 적으로 정의해야합니다. 워크 시트 이름을 나열하지 않아도되지만 Excel에서도 워크 시트에 대한 메타 데이터가 최상의 결과를 제공합니다.

누군가가 다른 방법을 알고 있지만 여러 가지 방법으로이 문제를 슬라이싱 한 경우이 두 가지 방법 중 하나에 부응하지 않는 답을 찾지 못했습니다.

+0

CLR 방식을 고려 중입니다 ... 아직 더 연구하고 연구해야합니다. 감사. – Temp

+0

@billinkc, 이것은 분명히 오래되었지만 나는 한 두 가지 방법을 우연히 발견했을 것입니다. –

11

이렇게하는 데는 최소한 두 가지 가능성이 있습니다. SQL Server 2005에서 이것을 확인하는 쉬운 방법이 없다는 것을 인정합니다. 단 2008 년입니다.

1 : 연결된 서버를 만들고 sp_tables_ex 및/또는 sp_columns_ex을 사용

-- Get table (worksheet) or column (field) listings from an excel spreadsheet 

-- SET THESE! 
declare @linkedServerName sysname = 'TempExcelSpreadsheet' 
declare @excelFileUrl nvarchar(1000) = 'c:\MySpreadsheet.xls' 
-- /SET 

-- Remove existing linked server (if necessary) 
if exists(select null from sys.servers where name = @linkedServerName) begin 
    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins' 
end 

-- Add the linked server 
-- ACE 12.0 seems to work for both xsl and xslx, though some might prefer the older JET provider 
exec sp_addlinkedserver 
    @server = @linkedServerName, 
    @srvproduct = 'ACE 12.0', 
    @provider = 'Microsoft.ACE.OLEDB.12.0', 
    @datasrc = @excelFileUrl, 
    @provstr = 'Excel 12.0;HDR=Yes' 

-- Grab the current user to use as a remote login 
declare @suser_sname nvarchar(256) = suser_sname() 

-- Add the current user as a login 
exec sp_addlinkedsrvlogin 
    @rmtsrvname = @linkedServerName, 
    @useself = 'false', 
    @locallogin = @suser_sname, 
    @rmtuser = null, 
    @rmtpassword = null 

-- Return the table/column info 
exec sp_tables_ex @linkedServerName 
exec sp_columns_ex @linkedServerName 

-- Remove temp linked server 
if exists(select null from sys.servers where name = @linkedServerName) begin 
    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins' 
end 

나는이 here에 대한 영감을 발견했다.

: 설명 된대로 Ole Automation Procedures를 사용하십시오. here. 나는 이걸 직접 시험해 보지 않았다.

관련 문제