同じ列で名前が異なる2つのExcelファイルがあります。
ソースから列名を見つけ、ターゲットと同じ列名を見つける必要があります。名前は異なる場合があります。値を比較して、一致する列名を見つける必要があります。
たとえば、1枚のExcelシートには
Order ID Quantity Units
--------- ------- --------
1022 7 55
もう1つは持つことができます
Order ID Qty Unt
-------- -------- --------
1022 7 55
したがって、データを比較することにより、数量と数量が同じであり、単位と数量が同じであることがわかります。
私はデータアダプターを使用し、Excelシートを読んで2つのデータテーブルに入力します。IDが同じ(注文ID)で、両方のデータテーブルを使用して値を比較することにより、2つの列がどのように一致するかを知りたいです。
string _basePath = @"C:\Users\Dev\Documents\Visual Studio 2010\Projects\Excel2\Excel2\";
string _targetConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _basePath + "Target.xlsx" + ";Extended Properties=Excel 12.0;";
string _sourceConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _basePath + "Source.xlsx" + ";Extended Properties=Excel 12.0;";
var adapter = new OleDbDataAdapter("SELECT * FROM [Target$]", _targetConnStr);
var ds = new DataSet();
adapter.Fill(ds, "targetTable");
DataTable _targetDataTable = ds.Tables["targetTable"];
adapter = new OleDbDataAdapter("SELECT * FROM [4028001$]", _sourceConnStr);
ds = new DataSet();
adapter.Fill(ds, "sourceTable");
DataTable _sourceDateTable = ds.Tables["sourceTable"];
foreach (DataRow row in _targetDataTable.Rows) // Loop over the rows.
{
foreach (var item in row.ItemArray) // Loop over the items.
{
Console.WriteLine(item);
}
}