检查 Excel 文件 Sheet 是否缺少列
Check Excel File Sheet for missing columns
我有一个 ssis 包,它接受一个 excel 文件并将其导入,但如果 sheet 中的任何列丢失,则导入失败。
我正在尝试编写一些代码来检查 sheet 中的列 header 以确保它包含一组列的列表,它不需要检查是否存在正确的列订单,只要它们存在。
目前我的代码如下
Dim strFile As String
strFile = Dts.Variables("User::found_file").Value.ToString
Dim xlConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & _
strFile & ";Extended Properties=""Excel 12.0 XML;HDR=YES"""
Dim xlConnection As New OleDbConnection(xlConnectionString)
xlConnection.Open()
Dim tablesInFile As DataTable = xlConnection.GetSchema("TABLES")
Dim currentTable As String
Dim columnsInTable As DataTable
Dim columnRestrictions(3) As String
Dim columnInTable As DataRow
Dim currentColumn As String
For Each tableInFile As DataRow In tablesInFile.Rows
currentTable = tableInFile.Item("TABLE_NAME").ToString
'tray header
If currentTable = "'Tray Header$'" Then
columnRestrictions(2) = currentTable
columnsInTable = xlConnection.GetSchema("COLUMNS", columnRestrictions)
end if
next
我需要一种简单的方法来检查所有列以确保它们都存在,而不必执行循环并一次检查一个列。
我需要它在缺少列时将失败布尔值标记为 true。
'tray header' sheet 包含的列是托盘 ID、托盘名称、描述、数量。
您可以使用简单的 SELECT
来验证您的文件:
Dim blnMissingColumns As Boolean = False
Dim cmdTest As OleDbCommand = xlConnection.CreateCommand
cmdTest.CommandText = "SELECT TOP 1 trayid, trayname, description, quantity FROM [Tray Header$]"
Try
cmdTest.ExecuteNonQuery()
Catch ex As Exception
If TypeOf ex Is OleDbException Then
If CType(ex, OleDbException).ErrorCode = -2147217904 Then
blnMissingColumns = True
End If
End If
End Try
cmdTest.Dispose()
我有一个 ssis 包,它接受一个 excel 文件并将其导入,但如果 sheet 中的任何列丢失,则导入失败。
我正在尝试编写一些代码来检查 sheet 中的列 header 以确保它包含一组列的列表,它不需要检查是否存在正确的列订单,只要它们存在。
目前我的代码如下
Dim strFile As String
strFile = Dts.Variables("User::found_file").Value.ToString
Dim xlConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & _
strFile & ";Extended Properties=""Excel 12.0 XML;HDR=YES"""
Dim xlConnection As New OleDbConnection(xlConnectionString)
xlConnection.Open()
Dim tablesInFile As DataTable = xlConnection.GetSchema("TABLES")
Dim currentTable As String
Dim columnsInTable As DataTable
Dim columnRestrictions(3) As String
Dim columnInTable As DataRow
Dim currentColumn As String
For Each tableInFile As DataRow In tablesInFile.Rows
currentTable = tableInFile.Item("TABLE_NAME").ToString
'tray header
If currentTable = "'Tray Header$'" Then
columnRestrictions(2) = currentTable
columnsInTable = xlConnection.GetSchema("COLUMNS", columnRestrictions)
end if
next
我需要一种简单的方法来检查所有列以确保它们都存在,而不必执行循环并一次检查一个列。
我需要它在缺少列时将失败布尔值标记为 true。
'tray header' sheet 包含的列是托盘 ID、托盘名称、描述、数量。
您可以使用简单的 SELECT
来验证您的文件:
Dim blnMissingColumns As Boolean = False
Dim cmdTest As OleDbCommand = xlConnection.CreateCommand
cmdTest.CommandText = "SELECT TOP 1 trayid, trayname, description, quantity FROM [Tray Header$]"
Try
cmdTest.ExecuteNonQuery()
Catch ex As Exception
If TypeOf ex Is OleDbException Then
If CType(ex, OleDbException).ErrorCode = -2147217904 Then
blnMissingColumns = True
End If
End If
End Try
cmdTest.Dispose()