关于sql server:从excel加载多个数据到sql SSIS

load multiple data from excel to sql SSIS

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

enter image description here

你所看到的问题是,如果 A 列被填充(例如:忽略行# 14 - X),文件的格式很糟糕,只处理/使用记录,我需要将 D1 中的值插入到日期中列。

有什么建议吗?

问候!


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

  • D1 获取日期值
  • 从第 4 行开始阅读
  • 忽略 Column1 为 NULL 的所有行
  • 解决方案

    1.从 D1

    获取日期值

  • 创建 2 个 SSIS 变量,@[User::FilePath](字符串类型)包含 excel 文件路径,@[User::FileDate](字符串类型)我们将使用它来存储日期值
  • 添加脚本任务,选择脚本语言为Visual Basic
  • 选择 @[User::FilePath] 作为 ReadOnly 变量,选择 @[User::FileDate] 作为 ReadWrite 变量
  • 打开脚本编辑器并使用以下代码检索日期值并将其存储到 @[User::FileDate]
  • 这将搜索名为 Refunds 的工作表并从中提取日期值并将该值存储到 @[User::FileDate]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
        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

  • 在DataFlow Task中添加一个Derived Column Transformation,添加一个具有以下表达式的派生列

    1
    @[User::FileDate]
  • 2.从第 4 行开始阅读

    因为我们假设 Excel 文件路径存储在 @[User::FilePath]

  • 首先打开 Excel Connection Manager 并取消选中框 First row has column names
  • 在DataFlow Task中,双击excel源
  • 将源设置为 SQL Command
  • 使用以下命令: SELECT * FROM [Refunds$A4:D] ,因此它将从第 4 行开始读取
  • 列名称将如下 F1 ... F4 ,在excel源中,您可以转到列选项卡并为列名称提供别名,因此在数据流任务中,它们将与其别名一起显示
  • 3.忽略 Column1 为 NULL 的所有行

  • 在 Excel 源代码后添加条件拆分
  • 根据以下表达式拆分流

    1
    ISNULL([F1]) == False
  • 如果你没有给F1一个别名,否则使用别名

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