我想比较两个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。用更多的代码编辑我的问题。谢谢 –