load multiple data from excel to sql SSIS
我正在使用 SSIS,我需要使用 SSIS 将具有以下 (Yellos) 格式的多个文件加载到 SQL
你所看到的问题是,如果 A 列被填充(例如:忽略行# 14 - X),文件的格式很糟糕,只处理/使用记录,我需要将 D1 中的值插入到日期中列。
有什么建议吗?
问候!
让我们把这个问题分成3个子问题:
解决方案
1.从 D1
获取日期值
这将搜索名为
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中添加一个
1 | @[User::FileDate] |
2.从第 4 行开始阅读
因为我们假设 Excel 文件路径存储在
3.忽略 Column1 为 NULL 的所有行
根据以下表达式拆分流
1 | ISNULL([F1]) == False |
如果你没有给
最后,请记住,您必须添加一个包含日期值的派生列(正如我们在第一个子问题中所说的那样)