Results 1 to 8 of 8

Thread: Compare two excel files using ClosedXML

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Compare two excel files using ClosedXML

    Hello,
    Can someone help me how i can compare two excel files.

    What i want to do is to take from File1.xlsx file specific columns to read
    Then to check does in File2 in specific column exists this records if not display in datagridview the error

    here is my code
    Code:
    Using workBook As XLWorkbook = New XLWorkbook(TxtPathLocation.Text + "\ConnectorListMST.xlsx")
                    Using workBook2 As XLWorkbook = New XLWorkbook(TxtPathLocation.Text + "\Production modules.xlsx")
                        Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
                        Dim workSheet2 As IXLWorksheet = workBook2.Worksheet(1)
                        Dim dt As DataTable = New DataTable()
                        Dim dt2 As DataTable = New DataTable()
                        Dim firstRow As Boolean = True
                        For Each row As IXLRow In workSheet.Rows()
                            For Each row2 As IXLRow In workSheet2.Rows()
                                If firstRow Then
                                    For Each cell As IXLCell In row.Cells()
                                        dt.Columns.Add(cell.Value.ToString())
                                    Next
                                    For Each cell2 As IXLCell In row2.Cells()
                                        dt2.Columns.Add(cell2.Value.ToString())
                                    Next
                                    firstRow = False
                                Else
                                    Dim cellA As IXLCell = row.Cell(1) 'Yazaki Number
                                    Dim cellB As IXLCell = row.Cell(2) 'TAB Folder
                                    Dim cellC As IXLCell = row.Cell(3) 'SYM File
                                    Dim cellE As IXLCell = row2.Cell(3) 'Material
                                    Dim cellD As IXLCell = row2.Cell(4) 'Yazaki Number
                                    Dim cellF As IXLCell = row2.Cell(6) 'Note
                                    Dim isLowerLetter As Boolean = False
                                    Dim isLowerLetter2 As Boolean = False
    
                                    If Not String.IsNullOrEmpty(cellA.Value) Then
                                        If String.IsNullOrEmpty(cellB.Value) Or String.IsNullOrEmpty(cellC.Value) Then
                                            isLowerLetter = True
                                        End If
                                    End If
    
                                    If String.IsNullOrEmpty(cellA.Value) Then
                                        If Not String.IsNullOrEmpty(cellB.Value) Or Not String.IsNullOrEmpty(cellC.Value) Then
                                            isLowerLetter = True
                                        End If
                                    End If
    
                                    If String.IsNullOrEmpty(cellA.Value) And String.IsNullOrEmpty(cellB.Value) And String.IsNullOrEmpty(cellC.Value) Then
                                        isLowerLetter = True
                                    End If
    
                                    If cellF.Value.ToString.Contains("Connector") Or cellF.Value.ToString.Contains("connector") Then
                                        If Not String.IsNullOrEmpty(cellE.Value) And Not String.IsNullOrEmpty(cellD.Value) Then
                                            If cellA.Value.ToString() <> cellD.Value.ToString() Then
                                                isLowerLetter2 = True
                                            End If
                                        End If
                                    End If
    
    
                                    If isLowerLetter Then
                                        If String.IsNullOrEmpty(cellA.Value) Then
                                            DataGridView2.Rows.Add("", "ConnectorListMST", "", cellB.Value.ToString() & "/" & cellC.Value.ToString(), "Found issue with missing connector information")
                                        Else
                                            DataGridView2.Rows.Add("", "ConnectorListMST", "", cellA.Value.ToString(), "Found issue with missing connector information")
                                        End If
                                    End If
                                    If isLowerLetter2 Then
                                        DataGridView2.Rows.Add("", "ConnectorListMST", "Production Modules", cellE.Value.ToString(), "Found Mismatch connector information")
                                    End If
                                End If
                            Next
                        Next
    https://ibb.co/QdbWLnD
    https://ibb.co/ZY9Ny0D

    So img1 is File1.xlsx and i want to read from column "D" the numbers and that answers to column "F" contains letter "connector".
    Then to check this numbers do they exists in File2.xlsx in column A
    Forgot to mention that in File1.xlsx it can contain same connectors with same numbers multiple times, to not use duplicates results in the end.
    Example:
    C88 6743434553
    C88 6743434553
    C88 6743434553
    C88 6743434553
    To read it only 1 time as result in the end if this number 6743434553 not exists in File2 column "A"
    Thats all
    Last edited by luckydead; Jul 14th, 2023 at 11:20 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width