none
Display Comparison between Text File and Excel File using C# RRS feed

  • Question

  • Hello friends.

    I am new with Visual C#. Now i am trying to make read a file in TEXT and EXCEL file  format and display the content in an application. Then, I must compare both TEXT and EXCEL file content and display the difference between both files. For example, just want to display name.

    However, i only can open TEXT and EXCEL file but can't do how to make comparison between both files.

    Can anyone suggest me any link that I can refer or sample program/codes to get the comparison between TEXT and EXCEL file and then display the difference?

    Thanks in advance.

    Tuesday, May 14, 2019 3:30 AM

Answers

  • Hi nurcahaya,

    Thanks for the feedback.

    According to your new reply, I have modified my code. You could refer to the following code.

     static void Main(string[] args)
            {
                var txt = File.ReadAllLines(@"D:\test.txt");
                Console.WriteLine("txt file:");
                List<string> list1 = new List<string>();
                foreach (var item in txt)
                {
                    Console.WriteLine(item);
                    list1.Add(item);
                }
                Console.WriteLine();
                Console.WriteLine("excel file:");
                List<string> list2 = new List<string>();
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:\test.xlsx");
                Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
                Excel.Range xlRange = xlWorksheet.UsedRange;
                int rowCount = xlRange.Row;
                int colCount = xlRange.Column;
                for (int i = 1; i <= rowCount+1; i++)
                {
                    for (int j = 1; j <= colCount+1; j++)
                    {
    
                        //write the value to the console
                        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                        {
                            Console.WriteLine(xlRange.Cells[i, j].Value2.ToString());
                            list2.Add(xlRange.Cells[i, j].Value2.ToString());
                        }
                          
    
                    }
                }
    
                var result = list1.Except(list2);
                Console.WriteLine();
                foreach (var item in result)
                {
                    Console.WriteLine("output is "+item);
                }
                Console.ReadKey();
            }

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

    • Marked as answer by nurcahaya Thursday, May 16, 2019 3:25 AM
    Tuesday, May 14, 2019 8:26 AM
    Moderator
  • Hello nurcahaya,

    You can refer Jack's code to read contents of both the files in a list then using Except(), you can make the difference. For example,

    List<string> result1 = list1.Except(list2);

    List<string> result2 = list2.Except(list1);

    List<string> finalResult = new List<string>();

    finalResult.AddRange(result1);

    finalResult.AddRange(result2);

    Hope this should work.


    Regards, http://www.shwetalodha.blogspot.in/

    Tuesday, May 14, 2019 9:11 AM

All replies

  • Hi nurcahaya,

    Thank you for posting here.

    Based on your description, you want to compare the difference between text and excel file.

    You could try the following code.

    using System.IO;
    using Excel = Microsoft.Office.Interop.Excel;//add reference
    
            static void Main(string[] args)
            {
                var txt = File.ReadAllLines(@"D:\test.txt");
                Console.WriteLine("txt file");
                Console.WriteLine();
                foreach (var item in txt)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine();
                Console.WriteLine("excel file:");
                Console.WriteLine();
    
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:\test.xlsx");
                Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
                Excel.Range xlRange = xlWorksheet.UsedRange;
                int rowCount = xlRange.Row;
                int colCount = xlRange.Column;
                for (int i = 1; i <= rowCount+3; i++)
                {
                    for (int j = 1; j <= colCount+1; j++)
                    {
                        //new line
                        if (j == 1)
                            Console.Write("\r\n");
    
                        //write the value to the console
                        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                        {
                            Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
                        }
                          
    
                        //add useful things here!   
                    }
                }
                Console.ReadKey();
            }
    

    txt file:

    excel file:

    Compare:

    If you have any question in my reply,please feel free to contact me.

    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 14, 2019 6:15 AM
    Moderator
  • If you can open them and want to display + compare them, you can load the files in 2 DataGridViews
    Tuesday, May 14, 2019 6:16 AM
  • Hi Jack, Thank you for your great explanation. 

    But, how about if I want to display the DIFFERENCE content for both file only? Not the whole input.. For example,

    TEXT FILE- 

    HELLO WORLD,

    I LIKE C#.

    MY NAME IS B.

    EXCEL FILE -

    HELLO WORLD,

    I LIKE C#.


    OUTPUT -

    My name is B.

    I already google but I can't find one that are works. I also find that the format excel file must change from .xlsx to .csv . Is it more easier? And sorry i can;t upload any screenshot bcs my account are not verify yet.

    Thank you.

    Tuesday, May 14, 2019 7:36 AM
  • Hi nurcahaya,

    Thanks for the feedback.

    According to your new reply, I have modified my code. You could refer to the following code.

     static void Main(string[] args)
            {
                var txt = File.ReadAllLines(@"D:\test.txt");
                Console.WriteLine("txt file:");
                List<string> list1 = new List<string>();
                foreach (var item in txt)
                {
                    Console.WriteLine(item);
                    list1.Add(item);
                }
                Console.WriteLine();
                Console.WriteLine("excel file:");
                List<string> list2 = new List<string>();
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:\test.xlsx");
                Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
                Excel.Range xlRange = xlWorksheet.UsedRange;
                int rowCount = xlRange.Row;
                int colCount = xlRange.Column;
                for (int i = 1; i <= rowCount+1; i++)
                {
                    for (int j = 1; j <= colCount+1; j++)
                    {
    
                        //write the value to the console
                        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                        {
                            Console.WriteLine(xlRange.Cells[i, j].Value2.ToString());
                            list2.Add(xlRange.Cells[i, j].Value2.ToString());
                        }
                          
    
                    }
                }
    
                var result = list1.Except(list2);
                Console.WriteLine();
                foreach (var item in result)
                {
                    Console.WriteLine("output is "+item);
                }
                Console.ReadKey();
            }

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

    • Marked as answer by nurcahaya Thursday, May 16, 2019 3:25 AM
    Tuesday, May 14, 2019 8:26 AM
    Moderator
  • Hello nurcahaya,

    You can refer Jack's code to read contents of both the files in a list then using Except(), you can make the difference. For example,

    List<string> result1 = list1.Except(list2);

    List<string> result2 = list2.Except(list1);

    List<string> finalResult = new List<string>();

    finalResult.AddRange(result1);

    finalResult.AddRange(result2);

    Hope this should work.


    Regards, http://www.shwetalodha.blogspot.in/

    Tuesday, May 14, 2019 9:11 AM