Excel 没有使用 oledb c# 更新值
Excel does not have updated values using oledb c#
我是 C# 的新手,正在尝试使用 OLEDB 将值写入 excel 文件。这里的问题是第一次如果该位置不存在 excel 文件它工作正常并将值打印到 excel,但第二次如果存在 excel 它确实不要覆盖 excel 或将新值写入 excel。我还需要 运行 循环执行此操作,因为我要打印四行而不是一行。需要帮助。
这是我的代码片段,我知道它只有一行要打印的代码,但我不知道添加循环的位置:
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 上打印的屏幕。
您需要检查table是否已经存在,如果是,那么只需添加记录即可。
这是一个工作示例
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.");
}
}
我是 C# 的新手,正在尝试使用 OLEDB 将值写入 excel 文件。这里的问题是第一次如果该位置不存在 excel 文件它工作正常并将值打印到 excel,但第二次如果存在 excel 它确实不要覆盖 excel 或将新值写入 excel。我还需要 运行 循环执行此操作,因为我要打印四行而不是一行。需要帮助。
这是我的代码片段,我知道它只有一行要打印的代码,但我不知道添加循环的位置:
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 上打印的屏幕。
您需要检查table是否已经存在,如果是,那么只需添加记录即可。
这是一个工作示例
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.");
}
}