用 SQL 指令碼讀取Excel 中的sheet數量及名稱

來源:互聯網
上載者:User

-- Get table (worksheet) or column (field) listings from an excel spreadsheet-- 設定變數declare @linkedServerName sysname = 'TempExcelSpreadsheet'declare @excelFileUrl nvarchar(1000) = 'D:\text.xlsx'-- /SET-- 刪除連結服務(如果它已經存在)if exists(select null from sys.servers where name = @linkedServerName) begin    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'end-- 添加服務物件-- ACE 12.0 可以很好地工作為*.xls 和 *.xlsx, 你也可以用 Jet ,但是只能訪問*.xls檔案exec sp_addlinkedserver    @server = @linkedServerName,    @srvproduct = 'ACE 12.0',    @provider = 'Microsoft.ACE.OLEDB.12.0',    @datasrc = @excelFileUrl,    @provstr = 'Excel 12.0;HDR=Yes'-- 擷取目前使用者declare @suser_sname nvarchar(256) = suser_sname()-- 添加目前使用者作為登陸這個連結服務exec sp_addlinkedsrvlogin    @rmtsrvname = @linkedServerName,    @useself = 'false',    @locallogin = @suser_sname,    @rmtuser = null,    @rmtpassword = null-- 返回 sheet 和 各個 sheet中的列exec sp_tables_ex @linkedServerNameexec sp_columns_ex @linkedServerName--if exists(select null from sys.servers where name = @linkedServerName) begin    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'end



出處:http://stackoverflow.com/questions/7439068/get-excel-sheet-name-in-sql-server


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.