当前位置: 首页 > 编程语言 > C#

比较两个Excel文件的不同点分享

时间:2023-04-10 13:07:33 C#

比较两个Excel文件的不同点我想比较两个输入的csv文件,看看有没有新增或删除行。解决这个问题的最佳方法是什么。我没有使用列名,因为所有文件的列名都不一致。privatevoidcompare_btn_Click(objectsender,EventArgse){stringfirstFile=firstExcel_txt.Text;var结果=ReadExcel(openFileDialog1);stringsecondFile=secondExcel_txt.Text;varresults2=ReadExcel(openFileDialog2);){var_excelFile=newExcelQueryFactory(openFileDialog.FileName);var_info=fromcin_excelFile.WorksheetNoHeader()选择c;字符串header1,header2,header3;foreach(varitemin_info){header1=item.ElementAt(0);header2=item.ElementAt(1);header3=item.ElementAt(2);}返回_info;任何关于我如何做到这一点的帮助都会很棒。我建议你为excel文件的每一行计算一个哈希值,然后你可以继续比较每一行的哈希值,看看它是否与另一个文件的任何哈希值匹配(见源代码中的注释)我也提供了创建的一些使用System.Security.Cryptography存储Excel文件内容的类;privatevoidcompare_btn_Click(objectsender,EventArgse){stringfirstFile=firstExcel_txt.Text;ExcelInfofile1=ReadExcel(openFileDialog1);stringsecondFile=secondExcel_txt.Text;ExcelInfofile2=ReadExcel(openFileDialog2);比较Excels(文件1,文件2);}publicvoidCompareExcels(ExcelInfofileA,ExcelInfofileB){foreach(ExcelRowrowAinfileA.excelRows){//如果fileA一行的当前hash在fileB中不存在则删除if(!fileB.ContainsHash(rowA.hash)){Console.WriteLine("删除行"+rowA.ToString());}}foreach(ExcelRowrowBinfileB.excelRows){//如果fileB一行的当前hash在fileA中不存在则添加if(!fileA.ContainsHash(rowB.hash)){Console.WriteLine("添加行"+rowB.ToString());}}}publicClassExcelRow{publicListlstCells;p公共字节[]散列公共ExcelRow(){lstCells=newList();}publicoverridestringToString(){stringresp;resp=string.Empty;foreach(lstCells中的字符串cellText){if(resp!=string.Empty){resp=resp+","+cellText;}else{resp=cellText;}}返回响应;}publicvoidCalculateHash(){byte[]rowBytes;字节[]单元格字节;诠释位置;整数行字节数;//确定存储单个excel行需要多少字节numRowBytes=0;foreach(lstCells中的字符串cellText){numRowBytes+=NumBytes(cellText);}//分配空间计算单行的HASHrowBytes=newbyte[numRowBytes]pos=0;//将每个单元格的cellText,转换为字节,拼接成一个单字节数组foreach(stringcellTextinlstCells){cellBytes=GetBytes(cellText);System.Buffer.BlockCopy(cellBytes,0,rowBytes,pos,cellBytes.Length);pos=cellBytes.Length;}hash=newMD5CryptoServiceProvider().ComputeHash(rowBytes);}静止的intNumBytes(stringstr){returnstr.Length*sizeof(char);}staticbyte[]GetBytes(stringstr){byte[]bytes=newbyte[NumBytes(str)];System.Buffer.BlockCopy(str.ToCharArray(),0,bytes,0,bytes.Length);返回字节;}}publicClassExcelInfo{publicListexcelRows;publicExcelInfo(){excelRows=newList();}publicboolContainsHash(byte[]hashToLook){boolfound;发现=假;foreach(ExcelRoweRowinexcelRows){found=EqualHash(eRow.hash,hashToLook);如果(发现){打破;}}返回找到;}publicstaticEqualHash(byte[]hashA,byte[]hashB){boolbEqual;诠释我;b等于=假;如果(hashA.Length==hashB.Length){i=0;while((i最准确的方法是将它们转换为字节数组,在转换为数组时检查两者的差异,使用以下链接获取有关如何将Excel工作表转换为字节数组的简单示例:ConvertExceltobyte[]现在您已经将两个Excel工作表都转换为byte[],您应该通过检查字节数组是否相等来检查字节数组是否不同您可以通过多种方式使用linq:使用System.Linq;//SequenceEqualbyte[]FirstExcelFileBytes=null;byte[]SecondExcelFileBytes=null;FirstExcelFileBytes=GetFirstExcelFile();SecondExcelFileBytes=GetSecondExcelFile();)==true){MessageBox.Show("数组相等");}else{MessageBox.Show("数组不匹配");有很多其他方法可以找到比较字节数组,你应该为你的研究做一些最好的。使用以下链接查看Rowaddedrowremovedetc.Comparingexcelsheets以上是C#学习教程:比较两个excel文件的不同之处所有分享的内容,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: