如何将具有不同名称和相同架构的Excel文件导入数据库? [英] How to import Excel files with different names and same schema into database?

查看:190
本文介绍了如何将具有不同名称和相同架构的Excel文件导入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

通过在控制流程中使用 ForEach循环容器 选项卡,然后在控制流任务内放置一个数据流任务。我在下面的例子中解释了这个过程。此示例使用 SQL Server 后端作为目标,并且 Excel 97-2003 格式。 xls 作为源文件。请注意表示Excel文件格式相同。



分步过程:


  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连接连接管理器中指向一个有效的Excel文件,如截图# 6 所示。


  5. 在指向SQL Server的连接管理器中创建一个 OLE DB连接 p>


  6. 在SSIS包中,将ForEach循环容器和数据流任务放在ForEach循环容器中,如截图# 7 所示。


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


  8. 在数据流任务内部,放置一个 Excel源来读取Excel文件数据和O LE DB目标将数据插入SQL Server表 dbo.Location 。数据流任务应该如截图# 10 所示。


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


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


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


  12. 示例执行数据流任务的截图显示在截图# 16


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


  14. 数据流任务属性中,将 DelayValidation 设置为 True ,以便当您打开包裹时,SSIS不会发生错误。


希望有所帮助。
$ b

截图#1:





截图#2:





截图# 3:



截图#4:





截图5:





截图#6:





截图#7:





截图#8:





截图#9:





截图#10:





截图#11:





截图#12:





截图#13:



截图#14:





截图#15:





截图#16:





截图#17:





截图#18:




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.,)

解决方案

One possible way of achieving this is by using ForEach Loop container in the Control Flow tab and then placing a Data Flow task within the Control Flow task. I have explained the process in the below example. This example uses SQL Server back-end as the destination and Excel 97-2003 format .xls as the source files. Please note that the Excel files should be of same format.

Ste-by-step process:

  1. Create a table named dbo.Location as 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 FolderPath will contain the path where the Excel files are located; FileExtension will contain the Excel file extension (here in this case it is *.xls) and FilePath should 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 connection in the connection manager pointing to one valid Excel file as shown in screenshot #6.

  5. Create an OLE DB Connection in the connection manager pointing 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 #8 and #9. By doing this, variable User::FilePath will contain the full path Excel files located in the folder c:\temp\ with the help of variables FolderPath and FileExtension configured on the Collection section.

  8. Inside the data flow task, place an Excel source to read Excel file data and OLE DB destination to 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 #11 and #12 to read the data using Excel connection.

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

  11. On the Excel connection in the connection manager, configure the Expressions ExcelFilePath and ServerName as shown in screenshot #15.

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

  13. Screenshot #17 displays 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 #3 and #4.

  14. On the Data Flow task properties, Set the DelayValidation to True so that the SSIS doesn't throw errors when you open the package.

Hope that helps.

Screenshot #1:

Screenshot #2:

Screenshot #3:

Screenshot #4:

Screenshot #5:

Screenshot #6:

Screenshot #7:

Screenshot #8:

Screenshot #9:

Screenshot #10:

Screenshot #11:

Screenshot #12:

Screenshot #13:

Screenshot #14:

Screenshot #15:

Screenshot #16:

Screenshot #17:

Screenshot #18:

这篇关于如何将具有不同名称和相同架构的Excel文件导入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆