J'essaie de comparer deux fichiers Exel mais je n'arrive pas à contourner l'exception "Impossible de lancer l'objet de type 'System.Data.DataRow 'taper' System.String '. " Exception sur le code ci-dessous.Impossible de convertir l'objet de type 'System.Data.DataRow' en type 'System.String'
Le problème est que les données d'intersection ne sont pas ajoutées dans la variable d'intersection ci-dessous après avoir croisé les deux fichiers.
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);
}
La classe complète ici. Cette classe est initiée dans la classe Form1 On Click Click. après que l'utilisateur sélectionne deux fichiers Excel. Il prend le chemin du fichier à partir des zones de texte respectives pour le filepath
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;
}
Eh bien quels sont les types de 'existingLead' et' newLead'? Vous ne nous avez montré aucune des déclarations, ce qui rend très difficile de vous aider. –
Qu'est-ce que 'existingLead'? Qu'est-ce que 'newLead'? Vous devez modifier votre question pour inclure un [Exemple minimal, complet et vérifiable] (https://stackoverflow.com/help/mcve) – maccettura
ExistingLead et NewLead sont des ArrayLists qui contiennent des données provenant de la feuille exel respective. Éditera ma question avec plus de code. Merci –