比较两个单元格不按预期方式工作

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

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

在这里输入图像说明

以下是我用来循环列表的代码:

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; } } 

也许不漂亮,但是如果能做到这一点,我很高兴。

在Excel中,空白单元格返回零长度string,不为null 。 尝试replace如下的实例:

 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中起作用。

在VBA中,您也可以使用如下testing:

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