gpt4 book ai didi

.net - 更新 Excel 工作表 : Cannot update '(expression)' ; field not updateable

转载 作者:行者123 更新时间:2023-12-04 21:13:00 25 4
gpt4 key购买 nike

好的,所以我是 .NET 的新手,我在使用 ACE.OLEDB 连接更改 Excel 工作表中的更新值时遇到了问题。我不断收到错误消息:无法更新“(表达式)”;字段不可更新。所以我的工作表的结构是这样的:

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

公式字段包含一个公式,该公式在另一个 Excel 工作簿中查找符号,然后获取其 Assets 类别。问题是它不允许我更新该字段。我将列的格式更改为文本只是为了看看这是否是问题但没有改变。我不知道为什么我不能像添加文本一样添加公式?

下面是我的代码:
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 示例的变体。

最佳答案

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 的连接也是如此。

关于.net - 更新 Excel 工作表 : Cannot update '(expression)' ; field not updateable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com