简体   繁体   English

将多个Excel文件/工作表导入SQL表

[英]Import multiple Excel files/sheets into SQL Table

I have about 100 xlsx files, all with 1-7 sheets each. 我大约有100个xlsx文件,每个文件都有1-7张纸。 Each file and sheet has the same columns as the table I want to import everything into. 每个文件和工作表都与我要将所有内容导入到的表的列相同。

I can use this successfully: 我可以成功使用它:

SELECT *
FROM OPENROWSET(
  'Microsoft.ACE.OLEDB.12.0',
  'Excel 12.0;Database=C:\0.xlsx',
  'SELECT * FROM [sheet1$]'
)

or 要么

SELECT * FROM OPENDATASOURCE( 'Microsoft.ACE.OLEDB.12.0', 'Data Source="C:\0.xlsx";
Extended properties=Excel 8.0')...Sheet1$

But how can I import multiple sheets from a file? 但是,如何从一个文件导入多张图纸?

  1. Create linked server for the Excel file 为Excel文件创建链接服务器
  2. Use sp_tables_ex to discover the tables that the provider returns (mostly this should be the worksheet names but it may depend on the provider implementation) 使用sp_tables_ex发现提供程序返回的表(大多数情况下应该是工作表名称,但这可能取决于提供程序的实现)

Linked Server 链接服务器

EXEC sp_addlinkedserver
    @server = 'ExcelServer1',
    @srvproduct = 'Excel',
    @provider = 'Microsoft.Jet.OLEDB.4.0',
    @datasrc = 'C:\Test\excel-sql-server.xls',
    @provstr = 'Excel 8.0;IMEX=1;HDR=YES;'

EXEC sp_dropserver
    @server = N'ExcelServer1',
    @droplogins='droplogins'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM