OleDbConnection myOleConn = new System.Data.OleDb.OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.txtInput.Text + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"); //编写与Excel相连接的字符串
string sql = "insert into [Sheet1$] values(" + this.textBox2.Text + "," + this.textBox3.Text +"," + this.textBox4.Text +"," + this.textBox5.Text +")"; //用SQL语句向Excel中的sheet1表插入数据
OleDbCommand ocm=new OleDbCommand(sql,myOleConn);
myOleConn.Open();
ocm.ExecuteNonQuery();
myOleConn.Close();
这是一个向Excel中插入数据的过程,但是系统总是提示ocm.ExecuteNonQuery();错误,我不知道哪里出错,请大家指教,并且在sql=sql = "insert into [Sheet1$] values('" + this.textBox2.Text + "','" + this.textBox3.Text +"','" + this.textBox4.Text +"','" + this.textBox5.Text +"')";这样也不正确,是不是在插入到Excel表中,sql语句写得有问题?
同时是不是在往Excel中插入数据时和往数据库中插入数据的方式不一样呀?往Excel中插入数据要另外的方法才行?如果是的话怎么来修改?
[此贴子已经被作者于2007-6-6 18:30:58编辑过]