Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using assertions to compare two excel files

Im using Visual Studio to create an automated test that creates two excel sheets. As a final check, I need to compare the content of these two excel sheets and ensure that they are equal. Is there any way to do this with assertions?

Something like Assert.AreEqual(file1, file2);?

Any help or guidance would be appreciated!

like image 802
Pseudo Sudo Avatar asked Jul 15 '16 15:07

Pseudo Sudo


People also ask

How do I compare two Excel files for differences and matches?

Open Spreadsheet Compare. In the lower-left pane, choose the options you want included in the workbook comparison, such as formulas, cell formatting, or macros. Or, just Select All. On the Home tab, choose Compare Files.

How do I compare two Excel spreadsheets for matching names?

Select both columns of data that you want to compare. On the Home tab, in the Styles grouping, under the Conditional Formatting drop down choose Highlight Cells Rules, then Duplicate Values. On the Duplicate Values dialog box select the colors you want and click OK.

How do I compare two sets of data in Excel?

When comparing two lists of data, select both columns of data, press F5 key on the keyboard, select the “Go to special” dialog box. Then select “Row difference” from the options. Matching cells of data across the rows in the columns are in white color and unmatched cells appear in grey color.


1 Answers

Thanks to Mangist for guidance on this. Ive written the following to compare two excel files:

public bool compareFiles(string filePath1, string filePath2)
    {
        bool result = false;
        Excel.Application excel = new Excel.Application();

        //Open files to compare
        Excel.Workbook workbook1 = excel.Workbooks.Open(filePath1);
        Excel.Workbook workbook2 = excel.Workbooks.Open(filePath2);

        //Open sheets to grab values from
        Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook1.Sheets[1];
        Excel.Worksheet worksheet2 = (Excel.Worksheet)workbook2.Sheets[1];

        //Get the used range of cells
        Excel.Range range = worksheet2.UsedRange;
        int maxColumns = range.Columns.Count;
        int maxRows = range.Rows.Count;

        //Check that each cell matches
        for (int i = 1; i <= maxColumns; i++)
        {
            for (int j = 1; j <= maxRows; j++)
            {
                if (worksheet1.Cells[j, i].Value == worksheet2.Cells[j, i].Value)
                {
                    result = true;
                }
                else
                    result = false;
            }
        }


        //Close the workbooks
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Marshal.ReleaseComObject(range);
        Marshal.ReleaseComObject(worksheet1);
        Marshal.ReleaseComObject(worksheet2);
        workbook1.Close();
        workbook2.Close();
        excel.Quit();
        Marshal.ReleaseComObject(excel);

        //Tell us if it is true or false
        return result;
    }

And using an assertion to check result:

Assert.IsTrue(compareFiles(testFile, compareFile), "Output files do not match.");
like image 119
Pseudo Sudo Avatar answered Sep 29 '22 02:09

Pseudo Sudo