2017-10-10 56 views
0

我想比较两个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; 
    } 
+1

那么'existingLead'和'newLead'的类型是什么?您没有向我们展示任何声明,这很难帮助您。 –

+1

'existingLead'是什么?什么是'newLead'?您需要编辑您的问题以包含[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve) – maccettura

+0

ExistingLead和NewLead是包含来自各自exel表的数据的ArrayList。用更多的代码编辑我的问题。谢谢 –

回答

0

石膏和数据行实例之间:

existingLead.Cast<string>() ... 

告诉它你想要的数据行的山坳

existingLead["ColName"].Cast<string>() ... 

或existsinglead.item(“ColName”)或DataRow做到这一点。

+0

'existingLead'是一个DataRow? OP没有指定它是什么,所以这个答案很可能100%不准确...... – maccettura

+0

我的答案100%不准确?不会是第一次;-)请问我的兄弟。但有些东西告诉我它可能是一个数据行(提示神秘音乐) – FastAl

+0

我们一直在比较不同的excel文件,不知道表名是否指定 –

相关问题