2017-10-10 127 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文件,不知道表名是否指定 –

相關問題