【问题标题】:load multiple data from excel to sql SSIS将多个数据从excel加载到sql SSIS
【发布时间】:2017-11-27 18:30:43
【问题描述】:

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

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

有什么建议吗?

问候!

【问题讨论】:

  • 我提供了详细的解决方案,等待您的回复。看看

标签: sql-server excel ssis etl sql-server-data-tools


【解决方案1】:

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

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

解决方案

1.从 D1 获取日期值

  1. 创建 2 个 SSIS 变量,@[User::FilePath](字符串类型),其中包含我们将使用的 excel 文件路径,@[User::FileDate](字符串类型)存储日期值
  2. 添加脚本任务,选择脚本语言为Visual Basic
  3. 选择@[User::FilePath] 作为只读变量,选择@[User::FileDate] 作为读写变量
  4. 打开脚本编辑器并使用以下代码检索日期值并将其存储到@[User::FileDate]

这将搜索名为Refunds 的工作表并从中提取日期值并将此值存储到@[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,使用以下表达式添加一个派生列

    @[User::FileDate]
    

2.从第 4 行开始阅读

因为我们假设 Excel 文件路径存储在 @[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 源中,您可以转到“列”选项卡并为列名指定别名,因此在数据流任务中它们将显示为它们的别名李>

3.忽略 Column1 为 NULL 的所有行

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

    ISNULL([F1]) == False
    

如果您没有给 F1 提供别名,则使用别名

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

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多