如何将具有不同名称和相同架构的Excel文件导入数据库? [英] How to import Excel files with different names and same schema into database?
问题描述
如何从每次具有不同文件名的excel源文件将数据导入到SSIS中的一个sql server表中(示例excel文件名:abc123,123abc,ab123c等)
通过在控制流程中使用
选项卡,然后在控制流任务内放置一个 ForEach循环容器
数据流任务
。我在下面的例子中解释了这个过程。此示例使用 SQL Server
后端作为目标,并且 Excel 97-2003
格式。 xls
作为源文件。请注意表示Excel文件格式相同。
分步过程:
-
创建名为
dbo.Location
的表,如截图# 1 即可。此示例将通过读取具有相同布局的三个不同的Excel文件来填充此表。截图在程序包执行之前显示一个空表。 -
在路径
中创建两个Excel文件c:\temp\
如截图# 2 - # 4 所示。请注意,Excel文件的布局相同,但内容不同。 -
在SSIS包中,创建三个变量,如截图# 5 即可。变量
FolderPath
将包含Excel文件所在的路径;FileExtension
将包含Excel文件扩展名(在这种情况下,这是 *。xls )和FilePath
应该配置为指向一个有效的Excel文件(仅在Excel连接管理器的初始配置期间才需要)。 -
创建一个
Excel连接
在连接管理器中指向一个有效的Excel文件,如截图# 6 所示。 -
在指向SQL Server的连接管理器中创建一个
OLE DB连接
p> -
在SSIS包中,将ForEach循环容器和数据流任务放在ForEach循环容器中,如截图# 7 所示。
-
配置ForEach循环容器,如屏幕截图# 8 和# 9 所示。通过这样做,变量
User :: FilePath
将包含位于文件夹中的完整路径Excel文件c:\temp\
帮助变量FolderPath
和FileExtension
在Collection
部分。 -
在数据流任务内部,放置一个
Excel源
来读取Excel文件数据和OLE DB目标
将数据插入SQL Server表 dbo.Location 。数据流任务应该如截图# 10 所示。 -
配置Excel源,如屏幕截图# 11 和# 12 以使用Excel连接来读取数据。
-
如屏幕截图所示配置OLE DB目标# 13 和# 14 将数据插入到SQL Server数据库表中。
-
在连接管理器中的 Excel连接中,配置表达式
ExcelFilePath
和ServerName
,如截图# 15 所示。 -
示例执行数据流任务的截图显示在截图# 16 。
-
截图# 17 显示数据在表中dbo.Location包执行后。请注意,它包含截屏# 3 和# 4 中显示的Excel文件中的所有行。
-
在
数据流任务
属性中,将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:
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.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.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) andFilePath
should be configured to point to one valid Excel file (this is required only during the initial configuration of the Excel connection manager).Create an
Excel connection
in the connection manager pointing to one valid Excel file as shown in screenshot #6.Create an
OLE DB Connection
in the connection manager pointing to the SQL Server.On the SSIS package, place a ForEach Loop container and a Data Flow task within the ForEach loop container as shown in screenshot #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 folderc:\temp\
with the help of variablesFolderPath
andFileExtension
configured on theCollection
section.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.Configure the Excel source as shown in screenshots #11 and #12 to read the data using Excel connection.
Configure the OLE DB destination as shown in screenshots #13 and #14 to insert the data into SQL Server database table.
On the Excel connection in the connection manager, configure the Expressions
ExcelFilePath
andServerName
as shown in screenshot #15.Sample execution of the data flow task is shown in screenshot #16.
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.
On the
Data Flow task
properties, Set theDelayValidation
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屋!