2014-02-13 47 views
1

我正在处理一个应用程序以比较两个列表:一个Excel工作簿包含列表的修订版本1,另一个工作簿包含同一列表的修订版本2。两个列表具有相同的结构,这意味着:它们在列中具有相同的信息:例如列A始终是主键PK,列B是温度,列C是压力等。单元格中没有公式 目标是查找新列表中与同一单元格不同的单元格旧名单。当旧列表中的PK温度= 45,新列表中的温度= 50时,新列表中的单元格将以黄色突出显示。这使得在包含2000 * 120个单元的列表中更容易找到更新。比较两个单元格不能按预期方式工作

它适用于两个测试文件,但是当我尝试使用真正的列表时,我看到了奇怪的行为:在某些列中,这两个列表中的单元格都是空的,但我的应用仍然将它们标识为不同并将它们标记为黄色。

enter image description here

下面是我用循环直通名单代码:

public void Run(int i) 
    { 

     wsOldSheet = oldWorkBook.Sheets[OldSheetName]; 

     // Define the range where to search 
     PrimaryKeys = wsOldSheet.get_Range(RangeStart, RangeEnd); 

     if (EzDiff.Program.Logging == true) 
     { 
      EzDiff.Program.__logger.Info(".Started working on row on: " + i); 
     } 

     CurValue = wsNewSheet.Cells[i, ColumnPK].value; 
     if (EzDiff.Program.Logging == true) 
     { 
      EzDiff.Program.__logger.Info("..Primary key = " + CurValue.ToString()); 
     } 

     //1. Check if PK exists in mydata. If not: it's a new PK -> we mark it as new and continue with the next PK 

     // Find 
     firstFind = PrimaryKeys.Find(CurValue, missing, 
         Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, 
         Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, 
         missing, missing); 

     if (firstFind == null) 
     { 
      if (EzDiff.Program.Logging == true) 
      { 
       EzDiff.Program.__logger.Info("...Primary key was not found."); 
      } 
      wsNewSheet.Cells[i, ColumnPK].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
     } 
     else 
     { 
      FoundInRow = firstFind.Row; 

      if (EzDiff.Program.Logging == true) 
      { 
       EzDiff.Program.__logger.Info("...Primary key was found in row: " + FoundInRow); 
      } 

      for (int mCol = 1; mCol < MaxColumnToWork; mCol++) 
      { 

       if (wsOldSheet.Cells[FoundInRow, mCol].Value == null) 
       //if (String.IsNullOrEmpty(wsOldSheet.Cells[FoundInRow, mCol].Value.ToString())) 
       { 
        if (!(wsNewSheet.Cells[i, mCol].Value == null)) 
        //if (String.IsNullOrEmpty(wsNewSheet.Cells[i, mCol].Value.ToString())) 
        { 
         // * * Here the cells are marked in error! * * // 
         wsNewSheet.Cells[i, mCol].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
        } 
       } 
       else 
       { 
        if (wsNewSheet.Cells[i, mCol].Value == null) 
        //if (String.IsNullOrEmpty(wsOldSheet.Cells[FoundInRow, mCol].Value.ToString())) 
        { 
         wsNewSheet.Cells[i, mCol].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
        } 
        else 
        { 
         String strOld = wsOldSheet.Cells[FoundInRow, mCol].Value.ToString(); 
         String strNew = wsNewSheet.Cells[i, mCol].Value.ToString(); 

         if (strNew.CompareTo(strOld) != 0) 
         { 
          wsNewSheet.Cells[i, mCol].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
         } 
        } 
       } 

      } 

      if (EzDiff.Program.Logging == true) 
      { 
       EzDiff.Program.__logger.Info("....finished comparing all columns from row: " + FoundInRow); 
      } 
     } 
    } 

如果任何人都可以看到我出问题了,请让我知道!

**已解决** 当我深入挖掘并查看造成奇怪结果的单元格时,我注意到这些表单在一个表单中为NULL,而在另一个表单中为“(空)”。所以我解决了这个问题:

 private bool equiv(object obj1, object obj2, double tolerance) 
    { 
     if (((obj1 == null) && (obj2 == null)) || 
      ((obj1 == null) && (obj2 == "")) || 
      ((obj1 == "") && (obj2 == null))) 
     { 
      return true; 
     } 
     else if ((obj1 == null) || (obj2 == null)) 
     { 
      return false; 
     } 
    } 

也许不是很漂亮,但如果它做的工作,我很高兴。

回答

0

在Excel中,空白单元格返回零长度字符串,而不是null。尝试更换喜欢的实例:

if (wsOldSheet.Cells[FoundInRow, mCol].Value == null) 

有:

if (wsOldSheet.Cells[FoundInRow, mCol].Value == "") 

,看看有没有什么帮助。在VBA相当于是:

If wsOldSheet.Cells(FoundInRow, mCol).Value = "" Then 
    ... 
End If 

我提,因为我不是一个C#程序员,但我知道,这部作品在VBA。

+1

虽然它不是实际的解决办法,我还是把它标记为这样的,因为它指出了我在正确的方向。 Thanx,布兰登! – Ad123

+0

慷慨的你,谢谢。如何(或何时!)一个Excel单元格将返回一个“NULL”与零长度的字符串是超越我......从未在VBA中发生过。 –

1

在VBA中你还可以用像测试:

if isempty(wsOldSheet.Cells(FoundInRow, mCol)) then ... 

'or 

if wsOldSheet.Cells(FoundInRow, mCol) is nothing then ...