SQL 如何将具有不同名称和相同架构的 Excel 文件导入数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6190578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:48:11  来源:igfitidea点击:

How to import Excel files with different names and same schema into database?

sqlexcelssis

提问by Lrn

How to import data into a sql server table in SSIS from an excel source file that has different file names each time (sample excel filenames: abc123, 123abc,ab123c etc.,)

如何将数据从每次具有不同文件名的excel源文件导入SSIS中的sql server表(示例excel文件名:abc123、123abc、ab123c等,)

回答by

One possible way of achieving this is by using ForEach Loop containerin the Control Flowtab and then placing a Data Flow taskwithin the Control Flow task. I have explained the process in the below example. This example uses SQL Serverback-end as the destination and Excel 97-2003format .xlsas the source files. Please notethat the Excel files should be of same format.

实现此目的的一种可能方法是ForEach Loop containerControl Flow选项卡中使用,然后将 a 放置Data Flow task在控制流任务中。我已经在下面的例子中解释了这个过程。本示例使用SQL Server后端作为目标,Excel 97-2003格式.xls作为源文件。请注意,Excel 文件的格式应相同。

Ste-by-step process:

分步过程:

  1. Create a table named dbo.Locationas shown in screenshot #1. This example will populate this table by reading three different Excel files having the same layout. The screenshot shows an empty table before the package execution.

  2. Create two Excel files in path c:\temp\as shown in screenshots #2- #4. Notice that both the Excel files have the same layout but different content.

  3. On the SSIS package, create three variables as shown in screenshot #5. Variable FolderPathwill contain the path where the Excel files are located; FileExtensionwill contain the Excel file extension (here in this case it is *.xls) and FilePathshould be configured to point to one valid Excel file (this is required only during the initial configuration of the Excel connection manager).

  4. Create an Excel connectionin the connection managerpointing to one valid Excel file as shown in screenshot #6.

  5. Create an OLE DB Connectionin the connection managerpointing to the SQL Server.

  6. On the SSIS package, place a ForEach Loop container and a Data Flow task within the ForEach loop container as shown in screenshot #7.

  7. Configure ForEach loop container as shown in screenshots #8and #9. By doing this, variable User::FilePathwill contain the full path Excel files located in the folder c:\temp\with the help of variables FolderPathand FileExtensionconfigured on the Collectionsection.

  8. Inside the data flow task, place an Excel sourceto read Excel file data and OLE DB destinationto insert data into SQL Server table dbo.Location. Data flow task should look like as shown in screenshot #10.

  9. Configure the Excel source as shown in screenshots #11and #12to read the data using Excel connection.

  10. Configure the OLE DB destination as shown in screenshots #13and #14to insert the data into SQL Server database table.

  11. On the Excel connectionin the connection manager, configure the Expressions ExcelFilePathand ServerNameas shown in screenshot #15.

  12. Sample execution of the data flow task is shown in screenshot #16.

  13. Screenshot #17displays the data in the table dbo.Location after package execution. Please note that it contains all the rows present in Excel files shown in screenshots #3and #4.

  14. On the Data Flow taskproperties, Set the DelayValidationto Trueso that the SSIS doesn't throw errors when you open the package.

  1. 创建一个dbo.Location如屏幕截图 # 1所示命名的表。此示例将通过读取具有相同布局的三个不同 Excel 文件来填充此表。屏幕截图显示了包执行之前的一个空表。

  2. 在路径中创建两个 Excel 文件,c:\temp\如屏幕截图 # 2- # 4 所示。请注意,这两个 Excel 文件具有相同的布局但内容不同。

  3. 在 SSIS 包上,创建三个变量,如屏幕截图 # 5所示。变量FolderPath将包含 Excel 文件所在的路径;FileExtension将包含 Excel 文件扩展名(在本例中为*.xls)并且FilePath应该配置为指向一个有效的 Excel 文件(这仅在 Excel 连接管理器的初始配置期间需要)。

  4. Excel connection连接管理器中创建一个指向一个有效 Excel 文件的文件,如屏幕截图 # 6所示。

  5. OLE DB Connection连接管理器中创建一个指向 SQL Server。

  6. 在 SSIS 包上,在 ForEach 循环容器中放置一个 ForEach 循环容器和一个数据流任务,如屏幕截图 # 7所示。

  7. 配置 ForEach 循环容器,如截图 # 8和 # 9 所示。通过这样做,变量User::FilePath将包含在变量c:\temp\的帮助下位于文件夹中FolderPathFileExtension在该Collection部分配置的完整路径 Excel 文件。

  8. 在数据流任务中,放置一个Excel source读取 Excel 文件数据和 OLE DB destination将数据插入到 SQL Server 表dbo.Location 中。数据流任务应该如截图# 10所示。

  9. 如屏幕截图 # 11和 # 12所示配置 Excel 源以使用 Excel 连接读取数据。

  10. 如屏幕截图 # 13和 # 14所示配置 OLE DB 目标,以将数据插入 SQL Server 数据库表中。

  11. 在连接管理器中的Excel 连接上,配置表达式ExcelFilePathServerName如屏幕截图 # 15所示。

  12. 数据流任务的示例执行如屏幕截图# 16所示。

  13. 屏幕截图 # 17显示包执行后表 dbo.Location 中的数据。请注意,它包含屏幕截图# 3和# 4 中显示的Excel 文件中的所有行。

  14. Data Flow task属性上,将 设置DelayValidationTrue以便 SSIS 在您打开包时不会抛出错误。

Hope that helps.

希望有帮助。

Screenshot #1:

截图 #1:

1

1

Screenshot #2:

截图#2:

2

2

Screenshot #3:

截图 #3:

3

3

Screenshot #4:

截图 #4:

4

4

Screenshot #5:

截图 #5:

5

5

Screenshot #6:

截图 #6:

6

6

Screenshot #7:

截图 #7:

7

7

Screenshot #8:

截图 #8:

8

8

Screenshot #9:

截图 #9:

9

9

Screenshot #10:

截图 #10:

10

10

Screenshot #11:

截图 #11:

11

11

Screenshot #12:

截图 #12:

12

12

Screenshot #13:

截图 #13:

13

13

Screenshot #14:

截图 #14:

14

14

Screenshot #15:

截图 #15:

15

15

Screenshot #16:

截图 #16:

16

16

Screenshot #17:

截图 #17:

17

17

Screenshot #18:

截图 #18:

18

18