简体   繁体   English

Excel没有使用oledb C#的更新值

[英]Excel does not have updated values using oledb c#

I am new to C# and trying to write values to an excel file using OLEDB. 我是C#的新手,正尝试使用OLEDB将值写入Excel文件。 The problem here is the first time if there is no excel file present in that location it works fine and prints the value to excel, but the second time if the excel is present it does not overwrite or write a new value to the excel. 这里的问题是,第一次在该位置没有excel文件,它可以正常工作并将其打印到excel,但是第二次,如果存在excel,它不会覆盖或向excel写新值。 I also need to run this in a loop as I have four rows to print and not one. 我还需要循环运行此程序,因为我要打印四行而不是一行。 Need help with this. 需要帮助。

This is my code snippet and I know I it has code for only one row to print but I don't know the place to add the loop : 这是我的代码片段,我知道我只有一行要打印的代码,但是我不知道添加循环的位置:

            try
        {

            var connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLearn\ExcelWorkBook.xls;Extended Properties=Excel 8.0";
            var sqlText = "CREATE TABLE TimeData ([HH] INT, [MM] INT,[AM / PM] VARCHAR(10))";

            using (var excelConnection = new OleDbConnection(connectionString))
            {


                // data is an object so it works with DBNull.Value
                object HHValue = TimeInHH.Text;
                object MMValue = TimeInMM.Text;
                object AMPMValue = Combo1AMPM.Text;




                // Executing this command will create the worksheet inside of the workbook
                // the table name will be the new worksheet name
                using (var command = new OleDbCommand(sqlText, excelConnection))
                {
                    if (excelConnection.State != ConnectionState.Open) {
                        excelConnection.Open();
                        command.ExecuteNonQuery();
                    }
                }

                // Add (insert) data to the worksheet
                var commandText = $"Insert Into TimeData ([HH], [MM], [AM / PM]) Values (@PropertyOne, @PropertyTwo, @PropertyThree)";

                using (var command = new OleDbCommand(commandText, excelConnection))
                {
                    // We need to allow for nulls just like we would with
                    // sql, if your data is null a DBNull.Value should be used
                    // instead of null 
                    command.Parameters.AddWithValue("@PropertyOne", HHValue ?? DBNull.Value);
                    command.Parameters.AddWithValue("@PropertyTwo", MMValue ?? DBNull.Value);
                    command.Parameters.AddWithValue("@PropertyThree", AMPMValue ?? DBNull.Value);
                    command.ExecuteNonQuery();
                }
            }


        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
        finally
        {
            Console.WriteLine("Executing finally block.");
        }

This is the screen which I am capturing data from and would like to print on excel on click of Submit button. 这是我正在从中捕获数据的屏幕,并希望在单击“提交”按钮时在excel上打印。 在此处输入图片说明

You need to check if table already exist , If yes , then simply go for adding records. 您需要检查表是否已经存在 ,如果是,则只需添加记录即可。

Here is a working example 这是一个有效的例子

        try
        {
            var connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLearn\ExcelWorkBook.xls;Extended Properties=Excel 8.0";

            var sqlText = "CREATE TABLE TimeData ([HH] INT, [MM] INT,[AM / PM] VARCHAR(10))";

            using (var excelConnection = new OleDbConnection(connectionString))
            {
                object HHValue = TimeInHH.Text;
                object MMValue = TimeInMM.Text;
                object AMPMValue = Combo1AMPM.Text;

                // Executing this command will create the worksheet inside of the workbook
                // the table name will be the new worksheet name
                using (var command = new OleDbCommand(sqlText, excelConnection))
                {
                    if (excelConnection.State != ConnectionState.Open)
                    {
                        excelConnection.Open();
                        //check if table already exists.
                        var exists = excelConnection.GetSchema("Tables", new string[4] { null, null, "TimeData", "TABLE" }).Rows.Count > 0;

                        if(!exists)
                           command.ExecuteNonQuery();
                    }
                }

                // Add (insert) data to the worksheet
                var commandText = $"Insert Into TimeData ([HH], [MM], [AM / PM]) Values (@PropertyOne, @PropertyTwo, @PropertyThree)";

                using (var command = new OleDbCommand(commandText, excelConnection))
                {
                    // We need to allow for nulls just like we would with
                    // sql, if your data is null a DBNull.Value should be used
                    // instead of null 
                    command.Parameters.AddWithValue("@PropertyOne", HHValue ?? DBNull.Value);
                    command.Parameters.AddWithValue("@PropertyTwo", MMValue ?? DBNull.Value);
                    command.Parameters.AddWithValue("@PropertyThree", AMPMValue ?? DBNull.Value);
                    command.ExecuteNonQuery();
                }
            }


        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
        finally
        {
            Console.WriteLine("Executing finally block.");
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM