我想比較兩個Exel文件,但無法繞過異常「無法轉換對象類型'System.Data.DataRow '鍵入'System.String'。「 以下代碼有異常。無法投入'System.Data.DataRow'類型的對象鍵入'System.String'
問題是在相交兩個文件後,相交數據沒有被添加到相交變量中。
try
{
foreach (var i in existingLead.Cast<string>().Intersect(newLead.Cast<string>()))
{
intersection.Add(i);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "In Intersection Array", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
完整的類在這裏。此類在Form1類的On Button Click中啓動。用戶選擇兩個excel文件後。它需要的文件路徑從各自的文本框的文件路徑
class Compare
{
public ArrayList existingLead = new ArrayList();
public ArrayList newLead = new ArrayList();
public ArrayList intersection = new ArrayList();
public void FindDuplicates(string filePathExisting, string filePathNew)
{
List<DataTable> existingDataTableList = ImportExcel(filePathExisting);
List<DataTable> newDataTableList = ImportExcel(filePathNew);
foreach (var item in existingDataTableList)
{
foreach (DataRow row in item.Rows)
{
//add to array
existingLead.Add(row);
}
}
foreach (var item in newDataTableList)
{
foreach (DataRow row in item.Rows)
{
//add to array
newLead.Add(row);
}
}
try
{
foreach (var i in existingLead.Cast<string>().Intersect(newLead.Cast<string>()))
{
intersection.Add(i);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "In Intersection Array", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private List<DataTable> ImportExcel(string FileName)
{
List<DataTable> _dataTables = new List<DataTable>();
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FileName);
//Checking for the extentions, if XLS connect using Jet OleDB
if (_Extension != null)
{
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0";
}
//Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase) || _Extension != null)
{
_ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0";
}
else
{
MessageBox.Show("File extensoin must be .xls or .xlsx", "Incompatible File Type", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
DataTable dataTable = null;
using (OleDbConnection oleDbConnection =
new OleDbConnection(string.Format(_ConnectionString, FileName)))
{
if (oleDbConnection != null)
{
try
{
oleDbConnection.Open();
//Getting the meta data information.
//This DataTable will return the details of Sheets in the Excel File.
DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
foreach (DataRow item in dbSchema.Rows)
{
//reading data from excel to Data Table
using (OleDbCommand oleDbCommand = new OleDbCommand())
{
oleDbCommand.Connection = oleDbConnection;
oleDbCommand.CommandText = string.Format("SELECT * FROM [{0}]",
item["TABLE_NAME"].ToString());
using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
{
oleDbDataAdapter.SelectCommand = oleDbCommand;
dataTable = new DataTable(item["TABLE_NAME"].ToString());
oleDbDataAdapter.Fill(dataTable);
_dataTables.Add(dataTable);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Querying Data Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("Connection String Empty", "No Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
return _dataTables;
}
那麼'existingLead'和'newLead'的類型是什麼?您沒有向我們展示任何聲明,這很難幫助您。 –
'existingLead'是什麼?什麼是'newLead'?您需要編輯您的問題以包含[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – maccettura
ExistingLead和NewLead是包含來自各自exel表的數據的ArrayList。用更多的代碼編輯我的問題。謝謝 –