我在尝试插入 Excel 工作表时遇到错误
这是名为 Sheet1 的 Excel 工作表的布局:
id name
1 test
2 apple
3 pear
我想将此数据插入到 Excel 工作表中:
4 pineapple
下面是我的代码:
try{
string srcExcel = "C:\\Users\\Desktop\\insertTest.xlsx";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + srcExcel + "; Extended Properties = 'Excel 8.0;HDR=Yes;IMEX=1'");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Insert Into [Sheet1$] (id,name) Values('4','pineapple')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MessageBox.Show("Insert Success!");
MyConnection.Close();
}
catch (Exception eInnerData)
{
MessageBox.Show("Method: getInnerData " + eInnerData.ToString());
}
运行此代码后,抛出异常并捕获此错误消息:
Operation must use an updateable query
我正在使用 Microsoft Excel 2010
最佳答案
从连接字符串中删除 IMEX=1。还要确保该文件不是只读的,并且当前未在 Excel 中打开
就其值(value)而言,我建议使用 EPPlus 库之类的东西而不是 Access db 驱动程序来读写 xlsx 文件
关于c# - Excel:插入查询引发错误消息 "Operation must use an updateable query",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47677372/