简体   繁体   English

使用C#中的OLE DB将dateTimePicker值插入Excel日期单元格

[英]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.

相关问题 如何在Windows窗体中使用C#将选定的DateTimePicker值获取到Excel单元格 - How can i get the selected DateTimePicker Value to the Excel cell using C# in Windows Forms 无法使用datetimepicker C#在Firebird中插入日期 - Can't insert date in firebird using datetimepicker c# 如何在C#中将日期和时间插入DateTimePicker? - how to insert date & time to DateTimePicker in C#? 使用C#.NET查询Excel电子表格,而不使用Jet OLE DB - Query Excel spreadsheet with C# .NET without using Jet OLE DB 如何使用ole db C#从Excel工作表中选择特定的合并列 - How Select specific merge columns from excel sheet using ole db c# C#/ OLE DB:INSERT INTO语句中的语法错误 - C# / OLE DB: Syntax Error in INSERT INTO statement 使用C#和OLE DB在日期和时间段之间使用记录填充Datagrid - Populating a Datagrid with records between a date and time period - Using C# and OLE DB 使用 C# 设置 Excel 单元格值 - Set Excel Cell value using C# 如何在 C# 中使用 datetimepicker 将日期(长格式)插入到访问数据库中? (错误仅在日期部分) - how to insert date(long format) into access database using datetimepicker in c# ? (error is in date part only) 将日期类型的DataGridView单元格值传递给DateTimePicker的C#Windows窗体 - C# Windows Form Passing DataGridView Cell Value of type Date to DateTimePicker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM