[英]Insert dateTimePicker value to excel date cell using OLE DB in C#
I'm writing my first program, a database one, and I'm stuck on inserting a new row of info into my excel database file. 我正在编写我的第一个程序,一个数据库程序,并且一直坚持在Excel数据库文件中插入新的信息行。 The problem is each row of data has a date cell in the A column.
问题在于数据的每一行在A列中都有一个日期单元格。 I want to be able to choose a date in the dateTimePicker, input the rest of my data (name, job number ...etc )via text boxes and then on a button click insert all that data into a new row.
我希望能够在dateTimePicker中选择一个日期,通过文本框输入其余数据(名称,职位编号等),然后在按钮上单击以将所有数据插入新行。 I can do it using Interop but I don't like it because it opens up excel and takes too long.
我可以使用Interop来完成此操作,但我不喜欢它,因为它打开了excel并耗时太长。 My code at the moment works nicely for all the text box inputs but won't work for the date.
目前,我的代码对所有文本框输入均有效,但不适用于日期。 The Date column in my excel file is in "Date" format.
我的excel文件中的“日期”列为“日期”格式。
So my question is this, Can I use OLE DB to insert a dateTimePicker value into a date formatted excel cell? 所以我的问题是,我可以使用OLE DB将dateTimePicker值插入日期格式的Excel单元格吗?
Here's my code, really appreciate the help. 这是我的代码,非常感谢您的帮助。
private void button1_Click(object sender, EventArgs e)
{
if (label60.Text == "new")
{
try
{
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.ACE.OLEDB.12.0;Data Source=C:\\Users\\Declan\\Documents\\Declan\\output.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" ");
MyConnection.Open();
myCommand.Connection = MyConnection;
DateTime date1 = dateTimePicker1.Value;
string post1 = textBox3.Text;
string type1 = textBox4.Text;
string source1 = textBox5.Text;
string income1 = textBox6.Text;
string prof1 = textBox7.Text;
string jobnum1 = textBox8.Text;
sql = "Insert into [Database$] (date,postcode,type,source,income,profession,customerid) values('" + date1 + "','" + post1 + "','" + type1 + "','" + source1 + "','" + income1 + "','" + prof1 + "','" + jobnum1 + "')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
label60.Text = "edit";
}
}
For those interested I've changed the code to use oledb parameters. 对于那些感兴趣的人,我将代码更改为使用oledb参数。 No idea how this has fixed it but looks like the excel date column needed to take a string with the format dd/MM/yyyy
不知道如何解决此问题,但看起来像excel date列需要采用dd / MM / yyyy格式的字符串
private void button1_Click(object sender, EventArgs e)
{
if (label60.Text == "new")
{
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Declan\\Documents\\Declan\\output.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" ");
MyConnection.Open();
myCommand.Connection = MyConnection;
DateTime date1 = dateTimePicker1.Value;
string date2 = date1.ToString("dd/MM/yyyy");
myCommand.CommandText = "Insert into [Database$] ([date],postcode,type,source,income,profession,phonenumber) values(@date, @post, @type,@source,@income,@prof,@jobnum)";
myCommand.Parameters.AddWithValue("@date",date2);
myCommand.Parameters.AddWithValue("@post", textBox3.Text);
myCommand.Parameters.AddWithValue("@type", textBox4.Text);
myCommand.Parameters.AddWithValue("@source", textBox5.Text);
myCommand.Parameters.AddWithValue("@income", textBox6.Text);
myCommand.Parameters.AddWithValue("@prof", textBox7.Text);
myCommand.Parameters.AddWithValue("@jobnum", textBox8.Text);
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
label60.Text = "edit";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.