This article explores an SSIS package for importing multiple Excel files data into SQL Server tables.
本文探讨了用于将多个Excel文件数据导入SQL Server表的SSIS包。
介绍 (Introduction)
Recently I come across a situation in which third-party vendors send multiple Excel files to load data into SQL Server tables. We can use SSMS Import and Export Wizards for data import. You can read more about this in How to import/export data to SQL Server using the SQL Server Import and Export Wizard.
最近,我遇到一种情况,第三方供应商发送多个Excel文件以将数据加载到SQL Server表中。 我们可以使用SSMS导入和导出向导进行数据导入。 您可以在如何使用SQL Server导入和导出向导将数据导入/导出到SQL Server中阅读有关此内容的更多信息。
Suppose you received multiple Excel files (let’s say 50) for data import. You need to launch an import wizard 50 times and complete it. It is a manual, tedious, and time-consuming task.
假设您收到了多个Excel文件(比如说50个)用于数据导入。 您需要启动导入向导50次并完成导入。 这是一项手动,繁琐且耗时的任务。
As I started earlier in my articles, SSIS is a true friend for DBA and developers. It comes for rescue in such situations. Let’s explore the SSIS solution in this article.
正如我在文章前面所提到的,SSIS是DBA和开发人员的真正朋友。 在这种情况下可以进行救援。 让我们探索本文中的SSIS解决方案。
样本数据 (Sample data)
For this article, I want data import from the following Excel files:
对于本文,我希望从以下Excel文件导入数据:
Each Excel file contains one row, and source column shows the excel file name:
每个Excel文件包含一行,并且源列显示excel文件名:
先决条件 (Prerequisites)
- SQL Server Data Tools or Visual Studio 2019 SQL Server数据工具或Visual Studio 2019
- SQL Server instance SQL Server实例
创建一个SSIS包,用于从多个Excel文件导入数据 (Create an SSIS package for the data import from multiple Excel files)
First, we will create an SSIS package for importing a single Excel file data into the SQL Server table. Later, we will convert the same package and import multiple Excel files data using SSIS variables and parameters.
首先,我们将创建一个SSIS包,用于将单个Excel文件数据导入到SQL Server表中。 稍后,我们将转换相同的包并使用SSIS变量和参数导入多个Excel文件数据。
创建用于数据导入SQL表 (Create a SQL table for data import)
We require a SQL table that will have data from SSIS import operation. Execute the following script for creating a SQL table for data import:
我们需要一个SQL表,该表将包含来自SSIS导入操作的数据。 执行以下脚本来创建用于数据导入SQL表:
CREATE TABLE [dbo].[SSISDataImport](
[EmpID] [int] NULL,
[EmpName] [varchar](50) NULL,
[Source] [nvarchar](50) NULL
) ON [PRIMARY]
GO
添加源excel文件连接 (Add a Source excel file connection)
Open Visual Studio and create a new SSIS package project. In Control Flow, right-click and go to New Connection:
打开Visual Studio并创建一个新的SSIS包项目。 在“ 控制流”中 ,右键单击并转到“ 新建连接” :
It shows all available connection types. Click on EXCEL connection manager and add it:
它显示所有可用的连接类型。 单击EXCEL连接管理器并添加它:
In Excel Connection Manager, provide the path of Excel file, and it automatically selects the Microsoft Excel versions. The first row of the Excel sheet contains the column header; therefore, we have a check on the First row has column names option:
在Excel Connection Manager中 ,提供Excel文件的路径,它会自动选择Microsoft Excel版本。 Excel工作表的第一行包含列标题; 因此,我们在“ 第一行具有列名”选项中进行了检查:
Click OK, and it shows the Excel connection in the connections manager area:
单击OK ,它在连接管理器区域中显示Excel连接: