从Excel中加载多个数据到SQL SSIS

我正在使用SSIS,我需要使用SSIS将以下(Yellos)格式的多个文件加载到SQL中

在这里输入图像说明

你可以看到的问题是,文件有一个可怕的格式只处理/消耗logging如果列A填充(例如:忽略行#14 – X),我需要将值插入到date列中。

有什么build议?

问候!

让我们把这个问题分成3个子问题:

  1. D1获取date值
  2. 从第4行开始阅读
  3. 忽略Column1为NULL的所有行

1.从D1获取date值

  1. 创build2个包含Excel文件path@[User::FileDate] (stringtypes)的 @[User::FilePath] (stringtypes)的 SSISvariables,我们将使用它来存储date值
  2. 添加脚本任务,select脚本语言为Visual Basic
  3. select@[User::FilePath]作为ReadOnlyvariables@[User::FileDate]作为ReadWritevariables
  4. 打开脚本编辑器并使用以下代码检索date值并将其存储到@[User::FileDate]

这将search名为@[User::FileDate]的表并从中提取date值并将此值存储到@[User::FileDate]

  m_strExcelPath = Dts.Variables.Item("FilePath").Value.ToString Dim strSheetname As String = String.Empty Dim strDate as String = String.Empty m_strExcelConnectionString = Me.BuildConnectionString() Try Using OleDBCon As New OleDbConnection(m_strExcelConnectionString) If OleDBCon.State <> ConnectionState.Open Then OleDBCon.Open() End If 'Get all WorkSheets m_dtschemaTable = OleDBCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"}) 'Loop over work sheet to get the first one (the excel may contains temporary sheets or deleted ones For Each schRow As DataRow In m_dtschemaTable.Rows strSheetname = schRow("TABLE_NAME").ToString If Not strSheetname.EndsWith("_") AndAlso strSheetname.EndsWith("$") Then If Not strSheetname.Tolower.Contains("refunds") Then Continue For Using cmd As New OleDbCommand("SELECT * FROM [" & strSheetname & "A1:D1]", OleDBCon) Dim dtTable As New DataTable("Table1") cmd.CommandType = CommandType.Text Using daGetDataFromSheet As New OleDbDataAdapter(cmd) daGetDataFromSheet.Fill(dtTable) 'Get Value from column 4 (3 because it is a zero-based index strDate = dtTable.Rows(0).Item(3).ToString End Using End Using 'when the first correct sheet is found there is no need to check others Exit For End If Next OleDBCon.Close() End Using Catch ex As Exception Throw New Exception(ex.Message, ex) End Tr Dts.Variables.Item("FileDate").Value = strDate Dts.TaskResult = ScriptResults.Success End Sub 
  1. 在DataFlow任务中添加Derived Column Transformation ,使用以下expression式添加派生列

     @[User::FileDate] 

2.从行号4开始读取

正如我们假定Excel文件path存储在@[User::FilePath]

  1. 首先打开Excel Connection Manager ,取消选中First row has column names
  2. 在DataFlow任务中,双击excel源文件
  3. 将源设置为SQL Command
  4. 使用以下命令: SELECT * FROM [Refunds$A4:D] ,所以它将从第4行开始读取
  5. 列名将如下面F1 … F4所示,在Excel源代码中,您可以转到“列”选项卡并为列名赋予别名,因此在数据stream任务中,将显示其别名

3.忽略Column1为NULL的所有行

  1. 在Excel源之后添加一个条件分割
  2. 根据以下expression式拆分Flow

     ISNULL([F1]) == False 

如果你没有给别名,那么使用别名

最后,请记住,您必须添加包含date值的派生列(如我们在第一个子问题中所述)