Hello everyone, I need help with an excel problem. I have two excels, ‘Excel1’ & ‘Excel2’. I need to compare columns from the excel, taking Employee ID as the base column, columns name might vary a little, but the excel column name are to be compared based on the field name given,
the two excels are: Excel1.xlsx (8.9 KB) Excel2.xlsx (8.8 KB)
I am adding Excel1 data to queue and I am retrieving all the fields as string (NAME, ROLE, EMPLOYEE ID, SALARY,LOCATION)
now the problem statement is I need to enter the output in a template output file OutputFile.xlsx (8.6 KB), (above screenshot), if the fields value matches, it should be left empty, if not, corresponding values has to be written under ‘Excel1 value’ and ‘Excel2 value’ (NOTE: Employee ID has to be entered in the output file no matter what, for both).
Each employee will be having a separate OutputFile.xlsx.
Can comparison of the Excel2 column values with retrieved Excel1 string values, and writing the difference value in template output file be done through LINQ query by any chance, for the faster approach?
Hi @Neha_Kumari1,
Yes, this can be achieved using LINQ for better performance.
Read both Excel1 and Excel2 into dtExcel1 and dtExcel2 using Read Range.
Create a dictionary to map Excel1 and Excel2 column names.
For each row in dtExcel1, get the matching row from dtExcel2 using LINQ: matchedRow = dtExcel2.AsEnumerable().FirstOrDefault(Function(r) r("Employee ID").ToString.Trim = row("EMPLOYEE ID").ToString.Trim)
Initialize a new DataTable for the output using Build Data Table.
Loop through each mapped field, compare values from both sheets.
If values differ, write to “Excel1 value” and “Excel2 value” columns.
Always write “Employee ID” for both sides, even if other fields match.
Write the output to a separate Excel for each employee using Write Range.
hi @Mir.Jasimuddin , thanks for your response. For Excel1 I’m getting the values through transaction item, and I’m storing the fields in string using SpecificContent function, so there’ll be only dtExcel2, no data table for Excel1. Can you please clarify how can I match values according to this scenario using linq. And how can I write the corresponding values to the corresponding field names, as per the given screenshot format
Hey @Neha_Kumari1, Thanks for the clarification! Since Excel1 values are coming from transactionItem.SpecificContent, you can skip creating a DataTable for Excel1. Here’s how you can approach it:
Read Excel2 into a DataTable (dtExcel2).
Use LINQ to find the matching row from dtExcel2 using the Employee ID from the transaction item:
Create an output DataTable (using Build Data Table) with columns: Field Name, Excel1 Value, Excel2 Value.
Use a mapping (e.g., a dictionary) of field names between Excel1 and Excel2.
Loop through each field, compare values:
If transactionItem.SpecificContent("FieldX").ToString.Trim <> matchedRow("FieldY").ToString.Trim Then
' Add a row to the output table with field name and both values
End If
Write the output to Excel or a log file as needed.
hey @Mir.Jasimuddin , I am a bit confused, how would we be using this second one, matchedRow, it’s datarow type, right, we won’t be able able to print the values using For each as its DataRow and not array of DataRow
Then check if they differ and add to the output table. No need to iterate over matchedRow — just treat it like a single row and access columns by name.