简体   繁体   English

将多个 Excel 文件导入 SQL Server

[英]Import Multiple Excel files to SQL Server

I have about 40+ excel files that I would like to import to SQL Server 2012. The import wizard is a great tool but it only allows me to import one file at a time.我有大约 40 多个 excel 文件想要导入到 SQL Server 2012。导入向导是一个很棒的工具,但它一次只允许我导入一个文件。

I want to avoid using SSIS because the import should be relatively easy - basically it is just a direct copy-paste with the first row in the Excel files being the column names, and the Excel Files name = Table Name.我想避免使用 SSIS,因为导入应该相对容易 - 基本上它只是直接复制粘贴,Excel 文件中的第一行是列名,Excel 文件名 = 表名。

Is there any easy way to achieve this?有什么简单的方法可以实现这一目标吗?

Edited: Since the columns will change quite frequently, I would like to avoid creating the tables manually.编辑:由于列会经常更改,我想避免手动创建表。 The wizard is great because it will create the table for me automatically.该向导很棒,因为它会自动为我创建表。

A solution I used is to combine multiple Excel files into one with multiple sheets.我使用的一种解决方案是将多个 Excel 文件合并为一个包含多个工作表的文件。 This can be done by running a VBA script (for example How to merge Excel files with VBA - thanks to Svetlana Cheusheva ) then use the SSMS import wizard to load multiple sheets into multiple tables.这可以通过运行 VBA 脚本(例如如何将 Excel 文件与 VBA 合并 - 感谢 Svetlana Cheusheva )然后使用 SSMS 导入向导将多个工作表加载到多个表中来完成。 In my case, I have 160 files, but they are not very big — 100 to 10000 rows — and it worked perfectly for me.就我而言,我有 160 个文件,但它们不是很大——100 到 10000 行——而且它对我来说效果很好。

You can do it using OpenRowSet.您可以使用 OpenRowSet 来完成。 The OPENROWSET function can be referenced in the FROM clause of a query as though it is a table name.可以在查询的 FROM 子句中引用 OPENROWSET 函数,就像它是表名一样。

Read excel using the following SELECT statement together with the OPENROWSET function使用以下 SELECT 语句和 OPENROWSET 函数读取 excel

SELECT * 
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                'Excel 8.0;Database=C:\Source\Addresses.xls;IMEX=1',
                'SELECT * FROM [Sheet1$]')

To import this to a SQL Server table without using DTS, since you are able to read the data using the SELECT statement, you can simply add the INTO clause to insert the records into a new SQL Server table.要在不使用 DTS 的情况下将其导入 SQL Server 表,因为您可以使用 SELECT 语句读取数据,您只需添加 INTO 子句即可将记录插入到新的 SQL Server 表中。

SELECT * 
INTO [dbo].[Addresses_NewTable]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                'Excel 8.0;Database=C:\Source\Addresses.xls;IMEX=1',
                'SELECT * FROM [Sheet1$]')

Reference article:- http://www.sql-server-helper.com/tips/read-import-excel-file-p03.aspx参考文章:- http://www.sql-server-helper.com/tips/read-import-excel-file-p03.aspx

It may be more research than you want to put into your problem, but you might also look into using BIML to script out the tedious and numerous similar packages.这可能比您想对您的问题进行更多的研究,但您也可能会考虑使用 BIML 来编写繁琐且众多类似的程序包。

http://www.sqlservercentral.com/stairway/100550/ http://www.mssqltips.com/sqlservertip/3094/introduction-to-business-intelligence-markup-language-biml-for-ssis/ http://www.sqlservercentral.com/stairway/100550/ http://www.mssqltips.com/sqlservertip/3094/introduction-to-business-intelligence-markup-language-biml-for-ssis/

Use Kutools for Excel to merge the worksheets across workbooks into one worksheet in a new workbook and then load that.使用 Kutools for Excel 将跨工作簿的工作表合并到新工作簿中的一个工作表中,然后加载它。

https://www.extendoffice.com/product/kutools-for-excel/merge-excel-wordbooks.html#a4 https://www.extendoffice.com/product/kutools-for-excel/merge-excel-wordbooks.html#a4

That's what I'm doing.这就是我正在做的。

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

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