C# 比较两个未按预期工作的单元格
我正在开发一个应用程序来比较两个列表:一个Excel工作簿包含列表的修订版1,另一个工作簿包含同一列表的修订版2。两个列表具有相同的结构,这意味着:它们在列中具有相同的信息:例如,A列始终是主键PK,B列是温度,C列是压力,等等。单元格中没有公式 目标是在新列表中查找与旧列表中相同单元格不同的单元格。当旧列表中PK的温度为45,新列表中PK的温度为50时,新列表中的单元格将以黄色突出显示。这使得在包含2000*120个单元格的列表中更容易找到更新 它可以很好地处理两个测试文件,但当我尝试处理真实列表时,我看到了奇怪的行为:在某些列中,两个列表中的单元格都是空的,但我的应用程序仍然将它们标识为不同的,并将它们标记为黄色 下面是我用来循环浏览列表的代码:C# 比较两个未按预期工作的单元格,c#,excel,C#,Excel,我正在开发一个应用程序来比较两个列表:一个Excel工作簿包含列表的修订版1,另一个工作簿包含同一列表的修订版2。两个列表具有相同的结构,这意味着:它们在列中具有相同的信息:例如,A列始终是主键PK,B列是温度,C列是压力,等等。单元格中没有公式 目标是在新列表中查找与旧列表中相同单元格不同的单元格。当旧列表中PK的温度为45,新列表中PK的温度为50时,新列表中的单元格将以黄色突出显示。这使得在包含2000*120个单元格的列表中更容易找到更新 它可以很好地处理两个测试文件,但当我尝试处理真实
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);
}
}
}
也许不太漂亮,但如果它能胜任,我很高兴。在Excel中,空白单元格返回零长度字符串,而不是
null
。尝试替换以下实例:
if (wsOldSheet.Cells[FoundInRow, mCol].Value == null)
与:
看看这是否有帮助。VBA等效值为:
If wsOldSheet.Cells(FoundInRow, mCol).Value = "" Then
...
End If
我之所以提到这一点,是因为我不是C#程序员,但我知道这在VBA中是有效的。在VBA中,您还可以使用以下测试:
if isempty(wsOldSheet.Cells(FoundInRow, mCol)) then ...
'or
if wsOldSheet.Cells(FoundInRow, mCol) is nothing then ...
虽然它不是实际的解决方案,但我仍然将其标记为这样,因为它为我指明了正确的方向。坦克斯,布兰登!你真慷慨,谢谢。Excel单元格如何(或何时)返回
NULL
vs.我无法理解长度为零的字符串。。。在VBA中从未发生过这种情况。
If wsOldSheet.Cells(FoundInRow, mCol).Value = "" Then
...
End If
if isempty(wsOldSheet.Cells(FoundInRow, mCol)) then ...
'or
if wsOldSheet.Cells(FoundInRow, mCol) is nothing then ...