问题
// e,g, I want to insert 3 columns in 1st row and 10 columns in rest of the rows //I am Creating excel file with sheet name as MySheet // then updating value in 1st row, 1st cell of header as blank //then inserting data // Can anyone please help to insert data in excel without header
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
";Mode=ReadWrite;Extended Properties=\"Excel 12.0 XML;HDR=NO\"";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE [MySheet] (a string)";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE [MySheet$] SET F1 = \"\"";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [MySheet] (a) values ('ABC')" //<-----Getting error to insert
cmd.ExecuteNonQuery();
}
}
conn.Close();
回答1:
Just try [removed the (a)]
cmd.CommandText = "INSERT INTO [MySheet] values ('ABC')";
回答2:
I get the same error. There was a padded space ending column name in Excel. Removing that space from end of column name fixed the issue.
来源:https://stackoverflow.com/questions/16481096/the-insert-into-statement-contains-the-following-unknown-field-name-a-make-s