Professional Documents
Culture Documents
Developer Network Sign in
Subscriber portal
Get tools
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions. Learn More
Quick access
Archived Forums > Visual C#
6,720
Points
Question
Top 1%
Hi,
Jack J Jun I want to compare 2 excel sheet and save the results(differences) to the another
MSFT CSG Joined Nov 2018 file.
0
Jack J Jun's threads Sign
2 4 in to
Show activity vote Sheet 1
11
A B C D E
aa bb cc dd ee
1 2 4 5 7
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 1/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
1 2 4 5 7
1 3 5 6 8
Sheet 2
E B A C D
ee bb aa cc dd
7 4 1 4 5
8 3 1 55 6
As i have select 2 headings on each sheets. You can say make this heading primarily
and then search.
As i have to compare in the way that A and aa from 1st sheet and then find the
same 2 values(A and aa) from the another sheet.
Like on Sheet1, i select B and bb column, then i search on sheet2, if i found then i
start comparing the value. According to screenshot. AS sheet1 has 2 and 3 and
sheet2 has 4 and 3. here 2 and 4 are different so give the headings of the
difference value
and then both values.
like in RESULT
And so one.
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 2/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
adwikcool 0 Points
Answers
Hi adwikcool,
I have modified my code according to your advice, you could try the following
code.
0
Sign class Program
in to {
table.Columns.Add("FirstHeading", typeof(string));
table.Columns.Add("SecondHeading", typeof(string));
table.Columns.Add("ValueinSheet1", typeof(int));
table.Columns.Add("ValueinSheet2", typeof(int));
string a = column.ColumnName;
string b = column.ColumnName;
if (a == b)
if (lst1[i].ToString() != lst2[j].ToString())
test1 = lst1[i].ToString();
test2 = lst2[i].ToString();
if(name1==name2)
table = dv.ToTable(true);
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 4/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
ToArray();
lines.Add(header);
lines.AddRange(valueLines);
File.WriteAllLines("D:\\excel.csv", lines);
Console.WriteLine("success");
Console.ReadKey();
comm.Connection = conn;
da.SelectCommand = comm;
da.Fill(dt);
return dt;
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 5/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Result:
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Proposed as answer by
Jack J Jun Microsoft contingent staff
Friday, May 31, 2019 5:15 AM
Unproposed as answer by
Wendy Zang Microsoft contingent staff
Thursday, June 13, 2019 5:20 AM
Proposed as answer by
Wendy Zang Microsoft contingent staff
Thursday, June 13, 2019 5:20 AM
Marked as answer by
Alberto Poblacion MVP
Tuesday, June 18, 2019 6:29 PM
All replies
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 6/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
0 adwikcool 0 Points
Sign
in to
vote
Hi adwikcool,
Based on your description, you want to compare 2 excel sheet and save the
0
results(differences) to the another file.
Sign
in to
vote You could try the following code.
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("SecondName", typeof(string));
table.Columns.Add("Sheet1Number", typeof(int));
table.Columns.Add("Sheet2Number", typeof(int));
string a1 = sheet1.Rows[i]["A"].ToString();
string b1 = sheet1.Rows[i]["B"].ToString();
string b2 = sheet2.Rows[i]["B"].ToString();
string c1 = sheet1.Rows[i]["C"].ToString();
string c2 = sheet2.Rows[i]["C"].ToString();
string d1 = sheet1.Rows[i]["D"].ToString();
string d2 = sheet2.Rows[i]["D"].ToString();
string e2 = sheet2.Rows[i]["E"].ToString();
if(a1!=a2)
Console.WriteLine(a1+"**"+a2);
if (b1 != b2)
if (c1 != c2)
if (d1 != d2)
if (e1 != e2)
ToArray();
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 8/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
var header = string.Join(",", columnNames);
lines.Add(header);
lines.AddRange(valueLines);
File.WriteAllLines("D:\\excel.csv", lines);
Console.WriteLine("success");
Console.ReadKey();
//sheetName = "Sheet1";
comm.Connection = conn;
da.SelectCommand = comm;
da.Fill(dt);
return dt;
Result:
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 9/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Thanks Jack.
string a1 = sheet1.Rows[i]["A"].ToString();
string b1 = sheet1.Rows[i]["B"].ToString();
string b2 = sheet2.Rows[i]["B"].ToString();
string c1 = sheet1.Rows[i]["C"].ToString();
string c2 = sheet2.Rows[i]["C"].ToString();
string d1 = sheet1.Rows[i]["D"].ToString();
string d2 = sheet2.Rows[i]["D"].ToString();
string e1 = sheet1.Rows[i]["E"].ToString();
string e2 = sheet2.Rows[i]["E"].ToString();
if(a1!=a2)
Console.WriteLine(a1+"**"+a2);
if (b1 != b2)
if (c1 != c2)
if (d1 != d2)
if (e1 != e2)
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 11/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
on this code.
Heading of sheets are always different. and maybe on this case there is 5 column
7 columns.
You are adding A to sheet 1 then sheet 2. then compare the with specific value.
Values of heading are always different. Like on one excel sheet it is A And aa
May be on new excel sheet there is X and xx. then how can i add, i mean it shoul
Example.
case1:
Sheet1
A B C D E
aa bb cc dd ee
Sheet2
A B C D E
Fg
aa bb cc dd ee
ff gg
In this i have to give different data as well as data which is not in sheet1.
case2:
Sheet1
X Y Z W M
NOP
aa bb cc dd ee
ff gg hh
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 12/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
1 2 3 6 4 6 6 4
2 5 6 4 6 6 4 6
SHEET2:
X Y Z W M
NOPQ
aa ff cc dd ee
ff gg ll hh
1 24 6 4 6 6 4 6
2 57 4 6 6 4 6 2
Here Heading Y is on both the sheets but 2nd heading value is different(ff and bb
And Same for P(it have hh value in sheet1 and ll in sheet 2). so no need to check
Same for Y.
Thanks in advance.
adwikcool 0 Points
Hi adwikcool,
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("Sheet1Number", typeof(int));
table.Columns.Add("Sheet2Number", typeof(int));
string a=column.ColumnName;
string b = column.ColumnName;
if(a==b)
if((i==j)&&(!lst1.SequenceEqual(lst2)))
if(lst1[i].ToString() != lst2[j].ToString())
test1 = lst1[i].ToString();
test2= lst2[i].ToString();
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 14/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
}
table = dv.ToTable(true);
ToArray();
lines.Add(header);
lines.AddRange(valueLines);
File.WriteAllLines("D:\\excel.csv", lines);
Console.WriteLine("success");
Console.ReadKey();
//sheetName "Sheet1";
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 15/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
//sheetName = Sheet1 ;
comm.Connection = conn;
da.SelectCommand = comm;
da.Fill(dt);
return dt;
Result:
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 16/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
p y
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Its working fine for the 1st column name. but its not working with 2nd column
name.
0
I mean. If A from Sheet1 are same to Sheet2. then it will not check for aa and aa.
Sign
in to it ignores that value.
vote
For example:
Sheet1:
A B C D E
aa ff cc dd ee
A B C D E
aa bb cc dd ee
Here in case of B.B and B are same. but ff and bb are not same. if i run on this it
will ignore that and give the only different value.
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 17/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Thanks in advance.
adwikcool 0 Points
i have checked on your code. you are taking the column name
0 string b = column.ColumnName;
Sign
in to
vote
Like this.
adwikcool 0 Points
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 18/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Hi Jack,
Thanks.
0
Sign
in to
vote Friday, May 24, 2019 5:38 AM
adwikcool 0 Points
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 19/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Hi adwikcool,
I think my code has solved your problem. I want to know what your current
0
question is.
Sign
in to
vote >>There is also not a SecondName Column is not there on the result.CSV
I only want to let you know the difference between sheet1 and sheet2.
>>Is it possible to give the Exact column number instead taking the
columnName?
The reason that I take the columnName is that we need find the same column
name between sheet1 and sheet2, then we could compare the value in that
column. Because the column name order in sheet1 may be different with sheet2,
I don't use the exact column
number.
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 20/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Hi Jack,
Yes your code gives the difference between both the sheets.
0
Sign But i need the difference as well as SecondName(2nd heading comparison) too.
in to
vote
Thats why i mentioned on my first message all the requirements where i stuck
and didnot found any results.
adwikcool 0 Points
Your first code was perfect but you were adding all the values maunally.
But it should be add automatically because there are many values as i dont add it
manully and these values are changes everytime..
0
Sign
in to Monday, May 27, 2019 4:15 AM
vote
adwikcool 0 Points
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 21/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Hi adwikcool,
>>Your first code was perfect but you were adding all the values manually.
Yes, you are right. According to your advice, I modified code to add all the values
0
automatically. Therefore, I am not sure what you want now, if allowed, you can
Sign
in to make a video or gif to express the meaning.
vote
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Edited by
Jack J Jun Microsoft contingent staff
Tuesday, May 28, 2019 7:36 AM
Hi Jack,
Sheet 1
0
Sign
in to A B C D E
vote aa bb cc dd ee
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 22/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
1 2 4 5 7
1 3 5 6 8
Sheet 2
E B A C D
ee bb bb cc dd
7 4 1 4 5
8 3 1 55 6
RESULT Sheet
B bb
2 4
In Case of B, Headings in Sheet 2 are B and bb and headings in sheet2 are B and
bb. B and B are same and bb and bb are same. now it will check for the
difference in the vlaues(B'Coumns value).
i want to do all this automatically. On your 1st code. you added 2nd value
Manually. i need to put all these automatically. As i have many values so i cannot
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 23/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Manually. i need to put all these automatically. As i have many values
so i cannot
able to add manually.
Thanks again.
adwikcool 0 Points
Hi adwikcool,
I have modified my code according to your advice, you could try the following
code.
0
Sign class Program
in to {
table.Columns.Add("FirstHeading", typeof(string));
table.Columns.Add("SecondHeading", typeof(string));
table.Columns.Add("ValueinSheet1", typeof(int));
table.Columns.Add("ValueinSheet2", typeof(int));
string a = column.ColumnName;
string b = column.ColumnName;
if (a == b)
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 24/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
if (lst1[i].ToString() != lst2[j].ToString())
test1 = lst1[i].ToString();
test2 = lst2[i].ToString();
if(name1==name2)
table = dv.ToTable(true);
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 25/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
( )
ToArray();
lines.Add(header);
lines.AddRange(valueLines);
File.WriteAllLines("D:\\excel.csv", lines);
Console.WriteLine("success");
Console.ReadKey();
comm.Connection = conn;
da.SelectCommand = comm;
da.Fill(dt);
return dt;
}
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 26/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
}
Result:
Best Regards,
Jack
Please remember to click "Mark as Answer" the responses that resolved your
issue, and to click "Unmark as Answer" if not. This can be beneficial to other
community members reading this thread. If you have any compliments or
complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Proposed as answer by
Jack J Jun Microsoft contingent staff
Friday, May 31, 2019 5:15 AM
Unproposed as answer by
Wendy Zang Microsoft contingent staff
Thursday, June 13, 2019 5:20 AM
Proposed as answer by
Wendy Zang Microsoft contingent staff
Thursday, June 13, 2019 5:20 AM
Marked as answer by
Alberto Poblacion MVP
Tuesday, June 18, 2019 6:29 PM
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 27/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
Visual Studio
Programs
Microsoft Azure BizSpark (for startups)
United States (English) Newsletter Privacy & cookies Terms of use Trademarks Manage cookies © 2022 Microsoft
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 28/28