2013-07-23 69 views
1

好的,所以我是.NET新手,在使用ACE.OLEDB連接更改Excel工作表中的更新值時遇到了問題。我不斷收到錯誤:無法更新'(表達式)';字段不可更新。因此,我的工作表的結構是這樣的:更新Excel工作表:無法更新'(表達式)';字段不可更新

Symbol AssetClass MarketValue SharePrice 
ABC  formula  $1000  $10.50 
MSFT  formula  $2000  $12 

公式字段包含一個公式,查找在另一個Excel工作簿中的符號,然後抓住它的資產類別。問題是它不允許我更新該字段。我將列的格式更改爲文本,以查看這是否是問題,但沒有更改。我不知道爲什麼我不能像添加文本一樣添加公式。

這裏是我下面的代碼:

Private m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PATH_Workbook1 & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;MAXSCANROWS=3;READONLY=FALSE\""" 
Dim conn As New OleDbConnection(m_sConn1) 
conn.Open() 
Dim cmd As New OleDbCommand() 
cmd.Connection = conn 
Dim da As New OleDbDataAdapter("Select * From [HoldingsTest$]", conn) 
Dim ds As DataSet = New DataSet() 
da.Fill(ds, "HoldingsTable") 

' Generate the UpdateCommand and add the parameters for the command. 
da.UpdateCommand = New OleDbCommand("UPDATE [HoldingsTest$] SET AssetClass =?, MarketValue =?, SharePrice = ? WHERE Symbol = ?", conn) 
da.UpdateCommand.Parameters.Add("@AssetClass", OleDbType.VarChar).SourceColumn = "AssetClass" 
da.UpdateCommand.Parameters.Add("@MarketValue", OleDbType.Currency).SourceColumn = "MarketValue" 
da.UpdateCommand.Parameters.Add("@SharePrice", OleDbType.Currency).SourceColumn = "SharePrice" 
da.UpdateCommand.Parameters.Add("@Symbol", OleDbType.VarChar, 9, "Symbol") 

' Update records 
ds.Tables(0).Rows(1)("AssetClass") = "formula" 
ds.Tables(0).Rows(1)("MarketValue") = 101 
ds.Tables(0).Rows(1)("SharePrice") = 91 

' Apply the dataset changes to the actual data source (the workbook). 
da.Update(ds, "HoldingsTable") 

任何幫助將不勝感激。我真的很難過。上面的代碼是來自MSDN的示例的變體。

+0

錯誤發生在哪一行? – rwisch45

+0

@ rwisch45 da.Update(ds,「HoldingsTable」) – Justin

+0

嘗試僅使用da.Update(ds) – rwisch45

回答

5

它在這裏提到的How To Use ADO with Excel Data from Visual Basic or VBA

"You can edit Excel data with the normal ADO methods. Recordset fields which correspond to cells in the Excel worksheet containing Excel formulas (beginning with "=") are read-only and cannot be edited"

這也是OLEDB連接到Excel真實,很遺憾。

+0

冠軍。爲我節省了很多時間。 – Mehrad

+0

出於某種原因,Microsoft已將上述答案中鏈接的KnowledgeBase文章替換爲關於非常基本的ADO的博客類文章。文章,因爲它存在這個答案寫的時候可以找到[這裏](http://web.archive.org/web/20130704024651/http://support.microsoft.com/kb/257819) – barrowc

+0

謝謝你爲我們幫幫我。它救了我。 –