我想通过oledb将一些行插入到无标题excel表中。例如,通过以下查询插入到A,B,C和E列:
Insert into [Sheet1$] (A,B,C,E) values('0','a','zz''2019-09-09')";
然而,我收到一个错误,其中没有找到A列。
有谁能告诉我如何将行插入无头excel表?
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\excelFile.xlsx';Extended Properties=Excel 12.0;");
myCommand.Connection = MyConnection;
sql = "Insert into [Sheet1$] (A,B) values('0','a')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
答案 0 :(得分:0)
尝试将HDR=No
添加到连接字符串的扩展属性中,如:
MyConnection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\excelFile.xlsx';Extended Properties=Excel 12.0;HDR=No");
HTH
答案 1 :(得分:0)
将连接字符串更改为MyConnection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\excelFile.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO';");
后,我必须修改查询更新的列。
F1表示第一行,依此类推。
sql = "Insert into [Sheet1$] (F1,F2) values('5','e')";