[英]Check Excel File Sheet for missing columns
我有一個sis軟件包,該軟件包需要一個excel文件並將其導入,但是如果工作表中的任何列均缺失,則導入將失敗。
我試圖編寫一些代碼來檢查工作表中的列標題,以確保它包含一組設置的列列表,只要它們存在,就不需要檢查其順序是否正確。
我到目前為止的代碼如下
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(如果缺少列)。
“托盤標題”工作表中包含的列是紙盤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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.