You are on page 1of 28

11/4/22, 23:07 How to compare 2 Excel sheets in c#

We use optional cookies to improve your experience on


our websites, such as through social media connections,
and to display personalized advertising based on your
online activity. If you reject optional cookies, only

cookies necessary to provide you the services will be


Accept Reject Manage cookies
used. You may change your selection by clicking
“Manage Cookies” at the bottom of the page. Privacy
Statement Third-Party Cookies



Developer Network Sign in
Subscriber portal
Get tools

Downloads Programs Community Documentation

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions. Learn More

Ask a question Search related threads Search forum questions

Quick access

Answered by: How to compare 2 Excel sheets in c#

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.

if found then compare the values from both sheets.

and give the result(differences on the 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

B      bb      2(Sheet1)      4(Sheet2)

And so one.

Please help on this. i am really thankful to you for your help.

  

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#

Monday, May 20, 2019 8:00 PM

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 {

vote static void Main(string[] args)

DataTable sheet1 = datatable("D:\\Test1\\test.xlsx", "Sheet1");

DataTable sheet2 = datatable("D:\\Test1\\test.xlsx", "Sheet2");

DataTable table = new DataTable();

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 test1 = null;

string test2 = null;

foreach (DataColumn column in sheet1.Columns)

string a = column.ColumnName;

foreach (DataColumn column1 in sheet2.Columns)

string b = column.ColumnName;

if (a == b)

List<object> lst1 = (from d in sheet1.AsEnumerable() select d.Fi


List<object> lst2 = (from d in sheet2.AsEnumerable() select d.Fi
for (int i 0; i < lst1 Count; i++)
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 3/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
for (int i = 0; i < lst1.Count; i++)

for (int j = i; j < lst2.Count; j++)

if ((i == j) && (!lst1.SequenceEqual(lst2)))

if (lst1[i].ToString() != lst2[j].ToString())

test1 = lst1[i].ToString();

test2 = lst2[i].ToString();

string name1 = sheet1.Rows[0][a].ToString();

string name2 = sheet2.Rows[0][a].ToString();

if(name1==name2)

table.Rows.Add(a, name1,test1, test2);

DataView dv = new DataView(table);

table = dv.ToTable(true);

var lines = new List<string>();

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#

string[] columnNames = table.Columns.Cast<DataColumn>().

Select(column => column.ColumnName).

ToArray();

var header = string.Join(",", columnNames);

lines.Add(header);

var valueLines = table.AsEnumerable()

.Select(row => string.Join(",", row.ItemArray));

lines.AddRange(valueLines);

File.WriteAllLines("D:\\excel.csv", lines);

Console.WriteLine("success");

Console.ReadKey();

public static DataTable datatable(string path, string sheetName)

DataTable dt = new DataTable();

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data So


using (OleDbConnection conn = new OleDbConnection(connectionStri
{

using (OleDbCommand comm = new OleDbCommand())

comm.CommandText = "Select * from [" + sheetName + "$]";

comm.Connection = conn;

using (OleDbDataAdapter da = new OleDbDataAdapter())

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

MSDN Community Support

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

Wednesday, May 29, 2019 3:29 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

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#

Need to do this with C#. Without the use of any MACROS.

Monday, May 20, 2019 8:04 PM

0 adwikcool 0 Points
Sign
in to
vote

Hi adwikcool,

Thank you for posting here.

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.

static void Main(string[] args)

DataTable sheet1 = datatable("D:\\test.xlsx", "Sheet1");

DataTable sheet2 = datatable("D:\\test.xlsx", "Sheet2");

DataTable table = new DataTable();

table.Columns.Add("FirstName", typeof(string));

table.Columns.Add("SecondName", typeof(string));

table.Columns.Add("Sheet1Number", typeof(int));

table.Columns.Add("Sheet2Number", typeof(int));

for (int i = 0; i < sheet1.Rows.Count; i++)

string a1 = sheet1.Rows[i]["A"].ToString();

string a2= sheet2.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();


https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 7/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
string e1 = sheet1.Rows[i][ E ].ToString();

string e2 = sheet2.Rows[i]["E"].ToString();

if(a1!=a2)

Console.WriteLine(a1+"**"+a2);

table.Rows.Add("A", "aa", Convert.ToInt32(a1), Convert.ToInt32(a2


}

if (b1 != b2)

Console.WriteLine(b1 + "**" + b2);

table.Rows.Add("B", "bb", Convert.ToInt32(b1), Convert.ToInt32(b2


}

if (c1 != c2)

Console.WriteLine(a1 + "**" + a2);

table.Rows.Add("C", "cc", Convert.ToInt32(c1), Convert.ToInt32(c2)


}

if (d1 != d2)

Console.WriteLine(a1 + "**" + a2);

table.Rows.Add("D", "dd", Convert.ToInt32(d1), Convert.ToInt32(d2


}

if (e1 != e2)

Console.WriteLine(a1 + "**" + a2);

table.Rows.Add("E", "ee", Convert.ToInt32(e1), Convert.ToInt32(e2)


}

var lines = new List<string>();

string[] columnNames = table.Columns.Cast<DataColumn>().

Select(column => column.ColumnName).

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

var valueLines = table.AsEnumerable()

.Select(row => string.Join(",", row.ItemArray));

lines.AddRange(valueLines);

File.WriteAllLines("D:\\excel.csv", lines);

Console.WriteLine("success");

Console.ReadKey();

public static DataTable datatable(string path,string sheetName)

DataTable dt = new DataTable();

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data So


using (OleDbConnection conn = new OleDbConnection(connectionS
{

using (OleDbCommand comm = new OleDbCommand())

//sheetName = "Sheet1";

comm.CommandText = "Select * from [" + sheetName + "$]";

comm.Connection = conn;

using (OleDbDataAdapter da = new OleDbDataAdapter())

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

MSDN Community Support

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.

Tuesday, May 21, 2019 5:58 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

Thanks Jack.

I am really thankful to you that you helped. 

But there is one thing.


0
Sign You are putting manually data on the sheet.
in to
vote
it should read data of sheet automatically not manually.
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 10/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#

 
string a1 = sheet1.Rows[i]["A"].ToString();

string a2= sheet2.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);

table.Rows.Add("A", "aa", Convert.ToInt32(a1), Convert.ToInt32(a2


}

if (b1 != b2)

Console.WriteLine(b1 + "**" + b2);

table.Rows.Add("B", "bb", Convert.ToInt32(b1), Convert.ToInt32(b2


}

if (c1 != c2)

Console.WriteLine(a1 + "**" + a2);

table.Rows.Add("C", "cc", Convert.ToInt32(c1), Convert.ToInt32(c2)


}

if (d1 != d2)

Console.WriteLine(a1 + "**" + a2);

table.Rows.Add("D", "dd", Convert.ToInt32(d1), Convert.ToInt32(d2


}

if (e1 != e2)

Console.WriteLine(a1 + "**" + a2);

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#

table.Rows.Add("E", "ee", Convert.ToInt32(e1), Convert.ToInt32(e2)


}

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

i mean no need to check for 4 and 6.

Same for Y.

Thanks in advance.

Tuesday, May 21, 2019 9:14 PM

adwikcool 0 Points

Hi adwikcool,

Thanks for the feedback.

According to your advice, I have modified my code.


0
Sign You could try the following code.
in to
https://social.msdn.microsoft.com/Forums/en-US/0fcd17dc-de06-46d3-bb5a-9e588445f2b2/how-to-compare-2-excel-sheets-in-c?forum=csharpgeneral 13/28
11/4/22, 23:07 How to compare 2 Excel sheets in c#
y g
in to
vote
static void Main(string[] args)

DataTable sheet1 = datatable("D:\\test.xlsx", "Sheet1");

DataTable sheet2 = datatable("D:\\test.xlsx", "Sheet2");

DataTable table = new DataTable();

table.Columns.Add("FirstName", typeof(string));

table.Columns.Add("Sheet1Number", typeof(int));

table.Columns.Add("Sheet2Number", typeof(int));

string test1 = null;

string test2 = null;

foreach (DataColumn column in sheet1.Columns)

string a=column.ColumnName;

foreach (DataColumn column1 in sheet2.Columns)

string b = column.ColumnName;

if(a==b)

List<object> lst1 = (from d in sheet1.AsEnumerable() select d.Fi


List<object> lst2 = (from d in sheet2.AsEnumerable() select d.Fi
for (int i = 0; i < lst1.Count; i++)

for (int j = i; j < lst2.Count; j++)

if((i==j)&&(!lst1.SequenceEqual(lst2)))

if(lst1[i].ToString() != lst2[j].ToString())

test1 = lst1[i].ToString();

test2= lst2[i].ToString();

table.Rows.Add(a, test1, test2);

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

DataView dv = new DataView(table);

table = dv.ToTable(true);

var lines = new List<string>();

string[] columnNames = table.Columns.Cast<DataColumn>().

Select(column => column.ColumnName).

ToArray();

var header = string.Join(",", columnNames);

lines.Add(header);

var valueLines = table.AsEnumerable()

.Select(row => string.Join(",", row.ItemArray));

lines.AddRange(valueLines);

File.WriteAllLines("D:\\excel.csv", lines);

Console.WriteLine("success");

Console.ReadKey();

public static DataTable datatable(string path, string sheetName)

DataTable dt = new DataTable();

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data So


using (OleDbConnection conn = new OleDbConnection(connectionStri
{

using (OleDbCommand comm = new OleDbCommand())

//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.CommandText = "Select * from [" + sheetName + "$]";

comm.Connection = conn;

using (OleDbDataAdapter da = new OleDbDataAdapter())

da.SelectCommand = comm;

da.Fill(dt);

return dt;

Result:

Best Regards,

Jack

MSDN Community Support

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.

Wednesday, May 22, 2019 8:09 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

Thank again Jack for you help.

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#

and There is also not a SecondName Column is not there on the


result.CSV

Thanks in advance.

Wednesday, May 22, 2019 12:42 PM

adwikcool 0 Points

and one more thing Jack, 

i have checked on your code. you are taking the column name

0 string b = column.ColumnName;
Sign
in to
vote
 Like this. 

Is it possible to give the Exact column number instead taking the


columnName?

Wednesday, May 22, 2019 12:45 PM

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,

Any update on this?

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,

Thanks for the feedback.

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.

Hope my explanation could be helpful.

Best Regards,

Jack

MSDN Community Support

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.

Friday, May 24, 2019 6:46 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

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, 

Thanks for helping me.

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.

So please help on this.

Thanks again for your help.

Monday, May 27, 2019 4:12 AM

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

MSDN Community Support

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

Tuesday, May 28, 2019 7:36 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

Hi Jack,

Thanks for helping.

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

FirstHeading   SecondHeading   ValueinSheet1  ValueinSheet2

B                             bb             
             2                              4

These are the sheets.

i have 2 headings on the basis of that comparison starts.

As in case of A, Headings in Sheet1 are A and aa and Heading in sheet2 are A


and bb. as A and A are same but aa and bb are not same so it will not check for
further values(A'Columns Value).

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).

2 .....with 4 not same so it will reflect on the result sheet.(firstHeading = B and


SecondHeading=bb and ValuesinSheet1 = 2 and valueinSheet2 = 4).

3 and 3 are same. 

Now you understand what is the input?

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.

Let me know if you still dont get the input.

Thanks again.

Tuesday, May 28, 2019 7:47 PM

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 {

vote static void Main(string[] args)

DataTable sheet1 = datatable("D:\\Test1\\test.xlsx", "Sheet1");

DataTable sheet2 = datatable("D:\\Test1\\test.xlsx", "Sheet2");

DataTable table = new DataTable();

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 test1 = null;

string test2 = null;

foreach (DataColumn column in sheet1.Columns)

string a = column.ColumnName;

foreach (DataColumn column1 in sheet2.Columns)

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#

List<object> lst1 = (from d in sheet1.AsEnumerable() select d.Fi


List<object> lst2 = (from d in sheet2.AsEnumerable() select d.Fi
for (int i = 0; i < lst1.Count; i++)

for (int j = i; j < lst2.Count; j++)

if ((i == j) && (!lst1.SequenceEqual(lst2)))

if (lst1[i].ToString() != lst2[j].ToString())

test1 = lst1[i].ToString();

test2 = lst2[i].ToString();

string name1 = sheet1.Rows[0][a].ToString();

string name2 = sheet2.Rows[0][a].ToString();

if(name1==name2)

table.Rows.Add(a, name1,test1, test2);

DataView dv = new DataView(table);

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#
( )

var lines = new List<string>();

string[] columnNames = table.Columns.Cast<DataColumn>().

Select(column => column.ColumnName).

ToArray();

var header = string.Join(",", columnNames);

lines.Add(header);

var valueLines = table.AsEnumerable()

.Select(row => string.Join(",", row.ItemArray));

lines.AddRange(valueLines);

File.WriteAllLines("D:\\excel.csv", lines);

Console.WriteLine("success");

Console.ReadKey();

public static DataTable datatable(string path, string sheetName)

DataTable dt = new DataTable();

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data So


using (OleDbConnection conn = new OleDbConnection(connectionStri
{

using (OleDbCommand comm = new OleDbCommand())

comm.CommandText = "Select * from [" + sheetName + "$]";

comm.Connection = conn;

using (OleDbDataAdapter da = new OleDbDataAdapter())

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

MSDN Community Support

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

Wednesday, May 29, 2019 3:29 AM


Jack J Jun Wicresoft (MSFT CSG) 6,720 Points

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#

Dev centers Learning resources Community Support


Microsoft Virtual Academy Forums Self support
Windows
Channel 9 Blogs

Office MSDN Magazine Codeplex

Visual Studio
Programs
Microsoft Azure BizSpark (for startups)

Microsoft Imagine (for students)


More...

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

You might also like