我想讀一個Excel頁面,但我得到以下錯誤:VB.net讀取Excel的頁面
Input string was not in a correct format.Couldn't store <555-555-5555> in PHONE_NUM Column. Expected type is Double.
看來,OLEDB命令期待列的類型爲雙(但是,在事實上,這是一個電話號碼字符串)。我想我會在某處讀到這種情況,因爲該命令看起來像列中的第一對項目,並確定了該類型應該基於哪個類型。無論如何總是以字符串形式讀取所有值?
這是我使用的代碼:
Dim connection As OleDb.OleDbConnection
Dim command As OleDb.OleDbCommand
Dim adapter As New OleDb.OleDbDataAdapter
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myFile + ";Extended Properties=Excel 12.0;"
Dim pgOne As String = "Page1$"
Dim pgTwo As String = "Page2$"
Dim selectOne As String = "SELECT * FROM [" & pgOne & "] WHERE COL1<>''"
Dim selectTwo As String = "SELECT * FROM [" & pgTwo & "]"
connection = New OleDb.OleDbConnection(connString)
Try
If connection.State = ConnectionState.Open Then
Else
connection.Open()
End If
command = New OleDb.OleDbCommand(selectOne, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "Table1")
adapter.SelectCommand.CommandText = selectTwo
adapter.Fill(ds, "Table2")
Catch ex As OleDb.OleDbException
Finally
adapter.Dispose()
If (Not command Is Nothing) Then
command.Dispose()
End If
connection.Close()
End Try
感謝您的回覆。我嘗試了兩個建議,但得到相同的錯誤「無法找到可安裝的ISAM」。當試圖打開連接時。有什麼想法嗎? – jason
那麼,ISAM錯誤是固定的(http://stackoverflow.com/questions/8120699/could-not-find-installable-isam)但我仍然有問題。使用IMEX = 1不起作用。嘗試使用HDR = NO會導致出現另一個錯誤「沒有給出一個或多個必需參數的值」。 – jason
嘗試IMEX = 1; HDR = NO;在一起 - 仍然沒有骰子,那麼我會發現有關事物的Schema方面 –