【问题标题】:Excel does not have updated values using oledb c#Excel没有使用oledb c#更新的值
【发布时间】:2018-01-23 19:57:24
【问题描述】:

我是 C# 新手,正在尝试使用 OLEDB 将值写入 excel 文件。这里的问题是第一次,如果该位置没有 excel 文件,它可以正常工作并将值打印到 excel,但第二次如果存在 excel,它不会覆盖或向 excel 写入新值。我还需要循环运行它,因为我要打印四行而不是一行。需要帮助。

这是我的代码 sn-p,我知道它只有一行要打印的代码,但我不知道添加循环的位置:

            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.");
        }

这是我正在从中捕获数据并希望在单击提交按钮时在 Excel 上打印的屏幕。

【问题讨论】:

  • 如果表存在则创建表语句轰炸(在第二遍)...id假设是这样
  • 是的,这句话很有可能被炸。
  • 那么你需要在创建表之前检查表是否存在...如果存在...不要创建它

标签: c# excel oledb oledbconnection oledbcommand


【解决方案1】:

您需要检查表是否已经存在,如果是,则直接添加记录。

这是一个工作示例

        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.");
        }

    }

【讨论】:

  • 这很好,解决了我的excel问题之一,它更新了工作表。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多