即使在执行查询后,我也无法将数据添加到Excel工作表中 [英] I can't add the data into excel sheet even after executing query

查看:82
本文介绍了即使在执行查询后,我也无法将数据添加到Excel工作表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btn_submit_Click(object sender, EventArgs e)
   {
       try
       {

           string name = textBox1.Text;
           string filepath="D://"+name;

           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.Jet.OLEDB.4.0;Data Source="+filepath+".xls;Extended Properties=Excel 8.0;");
           MyConnection.Open();
           myCommand.Connection = MyConnection;
           string createsql = null;
           createsql = "CREATE TABLE [Sheet1$] (F1 char(255), F2 char(255))";
           myCommand.CommandText = createsql;
           myCommand.ExecuteNonQuery();
           //myCommand.CommandText = createsql;
           //myCommand.ExecuteNonQuery();
           sql = "Insert into [Sheet1$] (F1,F2) values('"+textBox1.Text+"','"+textBox2.Text+"')";
           myCommand.CommandText = sql;
           myCommand.ExecuteNonQuery();
           MyConnection.Close();
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.ToString());
       }
   }





我的尝试:



i编写了这段代码,将数据添加到Excel工作表中。虽然我的查询运行完美,但数据没有写入excel文件



What I have tried:

i have written this code to add data into excel sheet.though my query is running perfectly but data is not getting written in excel file

推荐答案

(F1 char(255),F2 char(255));
myCommand.CommandText = createsql;
myCommand.ExecuteNonQuery();
// myCommand.CommandText = createsql;
// myCommand.ExecuteNonQuery();
sql = 插入[Sheet1
(F1 char(255), F2 char(255))"; myCommand.CommandText = createsql; myCommand.ExecuteNonQuery(); //myCommand.CommandText = createsql; //myCommand.ExecuteNonQuery(); sql = "Insert into [Sheet1


(F1,F2)值(' + textBox1.Text + ',' + textBox2.Text + ') ;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
(F1,F2) values('"+textBox1.Text+"','"+textBox2.Text+"')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }





我的尝试:



i编写了这段代码,将数据添加到Excel工作表中。虽然我的查询运行完美,但数据没有写入excel文件



What I have tried:

i have written this code to add data into excel sheet.though my query is running perfectly but data is not getting written in excel file


拿一个查看MSDN文档:如何使用ADO.NET检索和修改Excel工作簿中的记录 [< a href =https://support.microsoft.com/en-us/kb/316934target =_ blanktitle =新窗口> ^ ]



第一眼看,你不需要这段代码:

Take a look at MSDN documentation: How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook[^]

On the first look, you don't need this piece of code:
createsql = "CREATE TABLE [Sheet1


这篇关于即使在执行查询后,我也无法将数据添加到Excel工作表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆