C#里面不能将数据插入到Excel表格中。请问是怎么回事。

2025-01-03 02:52:06
推荐回答(1个)
回答1:

你直接都执行了,都不确定是否添加成功了,如果失败了不就没的数据,就是正常的撒
string CPDM = this.CPDMtextBox.Text.Trim();
string name = this.ExcelNametextBox.Text.Trim();
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + ExcelPathtextBox.Text.Trim();
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
string sql = string.Format("insert into [" + name + "$] (CPDM) values ('{0}')", CPDM);//执行插入语句
OleDbCommand cmd = new OleDbCommand(sql, myConn);
int row=cmd.ExecuteNonQuery() ;//获取返回值,看看是否添加成功了
Tishilabel.Text = row>0?"保存数据到Excel成功":"保存数据到Excel失败";
myConn.Close();
Tishilabel.ForeColor = row>0?Color.Green:Color.Red;