Update Excel 2007 with OleDb

烈酒焚心 提交于 2019-12-11 15:44:43

问题


Attempting to execute an update command against an Excel 2007 file gives the error: Operation must use an updateable query. I'm using System.Data.OleDb with a connection string like this:

Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=""" & pathToFile & """;" & _
    "Extended Properties=""Excel 12.0;HDR=YES"""

I have tried setting ReadOnly=false but that gives Could not find installable ISAM. I have also tried setting Mode=ReadWrite and IMEX=1 which didn't seem to have any effect. My update command is like this:

 Dim cmd As OleDbCommand = con.CreateCommand()
    cmd.CommandText = "UPDATE [" + sheetName + "] SET [Quantity Error] = 'test' WHERE [Full Name] = 'Mr. Brown White'"

where sheetName was obtained from querying the excel schema. Is it possible to do what I am trying to? Where have I gone wrong?


回答1:


You usually need a $ sign after the worksheet name. Try:

cmd.CommandText = "UPDATE [" + sheetName + "$] SET [Quantity Error] = 'test' WHERE [Full Name] = 'Mr. Brown White'"

Names without $ signs are interpreted as named ranges and not as worksheet names




回答2:


I believe this is usually a permissions issue. Can you read/write to the file if you open it in Excel?




回答3:


Use this connection string this will work

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + reportFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;\";

Besides checking the permissions to your Excel file verify if the Extended Properties of the connection string contains the IMEX=1 expression. If yes, remove it.



来源:https://stackoverflow.com/questions/836971/update-excel-2007-with-oledb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!