C#操作Excel提示标准表达式中数据类型不匹配,何解?
代码如下:
string mac = MactextBox.Text.Trim();
string gujian = FirmWaretextBox.Text.Trim();
string Pib = PibtextBox.Text.Trim();
string HFID = HFIDtextBox.Text.Trim();
string name = biaomingtextBox.Text.Trim();
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + SavePathtextBox.Text.Trim();
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string sql = string.Format("insert into ["+name+"$] (A,B,C,D) values ('{0}','{1}','{2}','{3}')", mac,gujian,Pib,HFID);
OleDbCommand cmd = new OleDbCommand(sql,myConn);
cmd.ExecuteNonQuery();
myConn.Close();
我的插入语句之前在操作MySQL的时候是这么写的,没错误,但是操作Excel的时候,却提示"标准表达式中数据类型不匹配"。请问是什么原因。难道insert into语句哪里错误了吗? 展开
void fun1()
{
string mac = "11";
string gujian = "22";
string Pib = "33";
string HFID = "44";
string name = "sheet1";
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=D:\\1.xls";
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string sql = string.Format("insert into [" + name + "$] (A,B,C,D) values ('{0}','{1}','{2}','{3}')", mac, gujian, Pib, HFID);
OleDbCommand cmd = new OleDbCommand(sql, myConn);
cmd.ExecuteNonQuery();
myConn.Close();
}
重点来了:你的EXCEL表的第一行必须有字段A,B,C,D
你运行就没问题了。