Vig*_*r A 6 sql-server excel ssis etl sql-server-data-tools
我有SSIS包,它将excel文件加载到数据库中.我已创建Excel源任务以将Excel列名称映射到数据库表列名称并且其工作正常.
在极少数情况下,我们正在接收带有一些空格的excel文件列名称(例如:列名称为"ABC"但我们正在接收"ABC")并且导致映射问题并且SSIS失败.
是否有可能在不打开excel的情况下修剪列名称.
注意:页面名称将是动态的,列位置可能会更改(例如:列"ABC可能存在于第一行或第二行或..").
首先,我的解决方案基于@DrHouseofSQL和@Bhouse答案,所以你必须先阅读@DrHouseofSQL答案然后@BHouse答案然后继续这个答案
注意:页面名称将是动态的,列位置可能会改变(例如:列“ABC可能存在于第一行或第二行或......
这种情况有点复杂,可以使用以下解决方法来解决:
Delay Validation属性设置为 true)@[User::strQuery]“读写变量”和@[User::ExcelFilePath]“只读变量” (在脚本任务窗口中)注意:你必须导入System.Data.OleDb
在下面的代码中,我们搜索excel前15行来查找标题,如果在15行之后可以找到标题,则可以增加数量。我还假设列范围是从A到I
m_strExcelPath = Dts.Variables.Item("ExcelFilePath").Value.ToString
Dim strSheetname As String = String.Empty
Dim intFirstRow As Integer = 0
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
Using cmd As New OleDbCommand("SELECT * FROM [" & strSheetname & "A1:I15]", OleDBCon)
Dim dtTable As New DataTable("Table1")
cmd.CommandType = CommandType.Text
Using daGetDataFromSheet As New OleDbDataAdapter(cmd)
daGetDataFromSheet.Fill(dtTable)
For intCount As Integer = 0 To 15
If Not String.IsNullOrEmpty(dtTable.Rows(intCount)(0).ToString) Then
'+1 because datatable is zero based indexed, +1 because we want to start from the second row
intFirstRow = intCount + 2
End If
Next
End Using
If intFirstRow = 0 Then Throw New Exception("header not found")
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 Try
Dts.Variables.Item("strQuery").Value = "SELECT * FROM [" & strSheetname & "A" & intFirstRow.ToString & ":I]"
Dts.TaskResult = ScriptResults.Success
End Sub
Run Code Online (Sandbox Code Playgroud)
Select * from [Sheet1$A2:I]为变量分配默认值@[User::strQuery]@[User::strQuery]Delay Validation属性设置为True来自OP评论:sometimes excel with empty data will come.(i.e) we have only header row not not data... in that case it fails entire task
解决方案:
如果您的 Excel 文件不包含数据(仅包含标题),您必须执行以下步骤:
@[User::ImportFile])@[User::ImportFile]到脚本任务ReadWrite变量@[User::ImportFile]= True,否则@[User::ImportFile]= False写出下面的表达式
@[User::ImportFile] == True
Run Code Online (Sandbox Code Playgroud)注意:新的脚本任务代码为:
@[User::ImportFile] == True
Run Code Online (Sandbox Code Playgroud)
来自OP评论:is there any other work around available to process the data flow task without skipping all data flow task,Actually one of the task will log the filename and data count and all, which are missing here
解决方案:
@[User::ImportFile] == False (与第一个连接器的步骤相同)或者Data Flow Task,您可以添加一个Execute SQL Task来在日志表中插入一行,而不是添加另一个
| 归档时间: |
|
| 查看次数: |
2839 次 |
| 最近记录: |