[英]Excel does not have updated values using oledb c#
我是C#的新手,正尝试使用OLEDB将值写入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.");
}
您需要检查表是否已经存在 ,如果是,则只需添加记录即可。
这是一个有效的例子
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.