gpt4 book ai didi

c# - 如何使用 C# 更新 Excel 工作表

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:20 26 4
gpt4 key购买 nike

我有两个 Excel 工作表。

这是第一张纸。 enter image description here

这是第二张 enter image description here

在第一个工作表中有一个名为 Language 的列,在第二个工作表中也有一个类似的列。所以现在我想要根据他们的 ID 从第一张工作表到第二张工作表的语言数据。

下面是我的代码。

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.Jet.OLEDB.4.0;Data Source=D:\consolidated.xls;Extended Properties=Excel 8.0;");


MyConnection.Open();
myCommand.Connection = MyConnection;

sql = "Update [second$] set [second$].[Language] =[first$].[Language] where [first$].[ID]= [second$].[ID] ";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();

MyConnection.Close();

出现以下错误:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters.

我对 SQL 还不是很熟练,所以我的 SQL 语法也可能是错误的...但我不确定。

最佳答案

这是我能想到的最好的:)

        var connectionStringBuilder = new System.Data.OleDb.OleDbConnectionStringBuilder();
connectionStringBuilder.DataSource = @"c:\dev\tmp\consolidated.xlsx";
connectionStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connectionStringBuilder.Add("Extended Properties", "Excel 12.0;");

using (var connection = new System.Data.OleDb.OleDbConnection(connectionStringBuilder.ToString()))
{
connection.Open();
var updateCommand = connection.CreateCommand();
updateCommand.CommandText = "update [second$] S inner join [first$] F on S.ID = F.ID set S.Language = F.Language";
updateCommand.ExecuteNonQuery();
}

关于c# - 如何使用 C# 更新 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43385528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com