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
How to import Excel files with different names and same schema into database?
提问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 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 notethat the Excel files should be of same format.
实现此目的的一种可能方法是ForEach Loop container
在Control Flow
选项卡中使用,然后将 a 放置Data Flow task
在控制流任务中。我已经在下面的例子中解释了这个过程。本示例使用SQL Server
后端作为目标,Excel 97-2003
格式.xls
作为源文件。请注意,Excel 文件的格式应相同。
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 managerpointing to one valid Excel file as shown in screenshot #6.Create an
OLE DB Connection
in the connection managerpointing 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 #8and #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 #11and #12to read the data using Excel connection.
Configure the OLE DB destination as shown in screenshots #13and #14to insert the data into SQL Server database table.
On the Excel connectionin 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 #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.
On the
Data Flow task
properties, Set theDelayValidation
to Trueso that the SSIS doesn't throw errors when you open the package.
创建一个
dbo.Location
如屏幕截图 # 1所示命名的表。此示例将通过读取具有相同布局的三个不同 Excel 文件来填充此表。屏幕截图显示了包执行之前的一个空表。在路径中创建两个 Excel 文件,
c:\temp\
如屏幕截图 # 2- # 4 所示。请注意,这两个 Excel 文件具有相同的布局但内容不同。在 SSIS 包上,创建三个变量,如屏幕截图 # 5所示。变量
FolderPath
将包含 Excel 文件所在的路径;FileExtension
将包含 Excel 文件扩展名(在本例中为*.xls)并且FilePath
应该配置为指向一个有效的 Excel 文件(这仅在 Excel 连接管理器的初始配置期间需要)。Excel connection
在连接管理器中创建一个指向一个有效 Excel 文件的文件,如屏幕截图 # 6所示。OLE DB Connection
在连接管理器中创建一个指向 SQL Server。在 SSIS 包上,在 ForEach 循环容器中放置一个 ForEach 循环容器和一个数据流任务,如屏幕截图 # 7所示。
配置 ForEach 循环容器,如截图 # 8和 # 9 所示。通过这样做,变量
User::FilePath
将包含在变量c:\temp\
的帮助下位于文件夹中FolderPath
并FileExtension
在该Collection
部分配置的完整路径 Excel 文件。在数据流任务中,放置一个
Excel source
读取 Excel 文件数据和 OLE DB destination
将数据插入到 SQL Server 表dbo.Location 中。数据流任务应该如截图# 10所示。如屏幕截图 # 11和 # 12所示配置 Excel 源以使用 Excel 连接读取数据。
如屏幕截图 # 13和 # 14所示配置 OLE DB 目标,以将数据插入 SQL Server 数据库表中。
在连接管理器中的Excel 连接上,配置表达式
ExcelFilePath
,ServerName
如屏幕截图 # 15所示。数据流任务的示例执行如屏幕截图# 16所示。
屏幕截图 # 17显示包执行后表 dbo.Location 中的数据。请注意,它包含屏幕截图# 3和# 4 中显示的Excel 文件中的所有行。
在
Data Flow task
属性上,将 设置DelayValidation
为True以便 SSIS 在您打开包时不会抛出错误。
Hope that helps.
希望有帮助。
Screenshot #1:
截图 #1:
Screenshot #2:
截图#2:
Screenshot #3:
截图 #3:
Screenshot #4:
截图 #4:
Screenshot #5:
截图 #5:
Screenshot #6:
截图 #6:
Screenshot #7:
截图 #7:
Screenshot #8:
截图 #8:
Screenshot #9:
截图 #9:
Screenshot #10:
截图 #10:
Screenshot #11:
截图 #11:
Screenshot #12:
截图 #12:
Screenshot #13:
截图 #13:
Screenshot #14:
截图 #14:
Screenshot #15:
截图 #15:
Screenshot #16:
截图 #16:
Screenshot #17:
截图 #17:
Screenshot #18:
截图 #18: