返回在两个文件比较后添加或删除的记录
本文关键字:添加 删除 记录 比较 文件 两个 返回 | 更新日期: 2023-09-27 18:29:17
我正在比较两个文件,其中一个文件可能添加或删除了项目。我正在检查这两个文件之间是否有差异,如果有,那么无论添加还是删除了记录,这些差异是什么。我想退回那些记录(添加或删除)
根据我所拥有的内容,它告诉我文件是否已删除或添加项目,但它不会返回已添加或删除的项目。对我所缺少的任何帮助都将不胜感激。
foreach (ExcelRow rowA in fileA.excelRows)
{
if (!fileB.ContainsHash(rowA.rowHash))
{
MessageBox.Show("Files are NOT the same. Data was REMOVED.'n" + rowA.ToString());
}
}
foreach (ExcelRow rowB in fileB.excelRows)
{
if (!fileA.ContainsHash(rowB.rowHash))
{
MessageBox.Show("Row added" + rowB.ToString());
}
}
public List<ExcelRow> excelRows = new List<ExcelRow>();
public bool ContainsHash(byte[] hashToLook)
{
bool found;
found = false;
foreach (ExcelRow eRow in excelRows)
{
found = EqualHash(eRow.rowHash, hashToLook);
if (found)
{
break;
}
}
return found;
}
public static bool EqualHash(byte[] hashA, byte[] hashB)
{
bool bEqual ;
int i ;
bEqual = false;
if (hashA.Length == hashB.Length)
{
i = 0;
while ((i < hashA.Length) && (hashA[i] == hashB[i]))
{
i++ ;
}
if (i == hashA.Length)
{
bEqual = true;
}
}
return bEqual ;
}
读取文件:
public ExcelInfo ReadExcel(OpenFileDialog openFileDialog)
{
var _excelFile = new ExcelQueryFactory(openFileDialog.FileName);
var _info = from c in _excelFile.WorksheetNoHeader() select c;
ExcelRow excelRow;
ExcelInfo resp;
resp = new ExcelInfo();
foreach (var item in _info)
{
excelRow = new ExcelRow();
excelRow.lstCells.Add(item.ElementAt(0));
excelRow.lstCells.Add(item.ElementAt(1));
excelRow.lstCells.Add(item.ElementAt(2));
excelRow.lstCells.Add(item.ElementAt(3));
excelRow.lstCells.Add(item.ElementAt(4));
excelRow.lstCells.Add(item.ElementAt(5));
excelRow.lstCells.Add(item.ElementAt(6));
excelRow.lstCells.Add(item.ElementAt(7));
excelRow.lstCells.Add(item.ElementAt(8));
excelRow.lstCells.Add(item.ElementAt(9));
excelRow.lstCells.Add(item.ElementAt(10));
excelRow.lstCells.Add(item.ElementAt(11));
excelRow.lstCells.Add(item.ElementAt(12));
excelRow.CalculateHash();
resp.excelRows.Add(excelRow);
}
return resp;
}
计算哈希:
public void CalculateHash()
{
byte[] rowBytes;
byte[] cellBytes;
int pos;
int numRowBytes;
numRowBytes = 0;
foreach (string cellText in lstCells)
{
numRowBytes += NumBytes(cellText);
}
//Allocate space to calculate the HASH of a single row
rowBytes = new byte[numRowBytes];
pos = 0;
//Concatenate the cellText of each row into a single byte array
foreach (string cellText in lstCells)
{
cellBytes = GetBytes(cellText);
System.Buffer.BlockCopy(cellBytes, 0, rowBytes, pos, cellBytes.Length);
pos = cellBytes.Length;
}
rowHash = new MD5CryptoServiceProvider().ComputeHash(rowBytes);
}
调试时:
if (!fileB.ContainsHash(rowA.rowHash))
文件B包含三行,文件A包含四行。
fileB=3,rowA=文件A中的第一行,(.rowHash)是byte[16]
当我继续使用ContainHash方法时,byte[]hashToLook=16-这不应该是rowA吗?
excelRows=3(fileB)
那么EqualHash(eRow.rowHash,hashToLook)是(文件A中的第一行,byte[16])
我是从A排路过吗?
您就快到了,只需添加两个列表来跟踪在A中但不在B中的项目和在B中而不在A中的项目:
var notInA = new List<ExcelRow>();
var notInB = new List<ExcelRow>();
现在在您的代码中,将它们添加到适当的列表中:
foreach (ExcelRow rowA in fileA.excelRows)
{
if (!fileB.ContainsHash(rowA.rowHash))
{
MessageBox.Show("Files are NOT the same. Data was REMOVED.'n" + rowA.ToString());
notInB.Add(rowA);
}
}
foreach (ExcelRow rowB in fileB.excelRows)
{
if (!fileA.ContainsHash(rowB.rowHash))
{
MessageBox.Show("Row added" + rowB.ToString());
notInA.Add(rowB);
}
}
只需将hashA[i] != hashB[i]
更改为hashA[i] == hashB[i]
:)