Tamal Banerjee

Tamal Banerjee

  • NA
  • 25
  • 3.3k

Checking if specific columns data exists in a different workbook c# ..

May 20 2021 4:16 PM
I've got a couple of different workbooks 1) newData.xlsx & 2) DATABASE.xlsx (image and file attached below)
I'm trying to check whether the Bill No. & Bill Date of the first row (i.e. row 6, as the relevant data starts from row 6) & last rows of the newData.xlsx file already exist in the DATABASE.xlsx file. The column numbers of both the target cells are different in both files.
 
How can I do this in the most efficient way possible using spire.xls & c#?
 
I've tried the below
  1. string queryFrom=@"D:\sample\newData.xlsx";    
  2. string queryIn=@"D:\sample\Testing\DATABASE.xlsx";  
  3. Workbook workbook = new Workbook();  
  4. workbook.LoadFromFile(queryFrom);    
  5. Worksheet sheet = workbook.Worksheets[0];  
  6. var firstRowCellBillNo=sheet.Range["M6"].Value2.ToString().Trim();    
  7. var firstRowCellBillDate=sheet.Range["N6"].Value2.ToString().Trim();  
  8. var lastRowCellBillNo=sheet.Range["M"+(sheet.LastRow - 1)].Value2.ToString().Trim();    
  9. var lastRowCellBillDate=sheet.Range["N"+(sheet.LastRow - 1)].Value2.ToString().Trim();  
  10. Dictionary<stringstring> dictionary = new Dictionary<stringstring>();    
  11. Dictionary<stringstring> dictionary2 = new Dictionary<stringstring>();   
  12. if (!dictionary.ContainsKey(firstRowCellBillNo) && !dictionary.ContainsKey(lastRowCellBillNo))    
  13. {    
  14.         dictionary.Add(firstRowCellBillNo.ToString(), firstRowCellBillDate.ToString());    
  15.         dictionary.Add(lastRowCellBillNo.ToString(), lastRowCellBillDate.ToString());    
  16. }  
  17. workbook.Dispose();  
  18. Workbook wbk=new Workbook();  
  19. wbk.LoadFromFile(queryIn);    
  20. Worksheet sh=wbk.Worksheets[0];  
  21. CellRange xlRange=sh.Range[1,1,sh.LastRow,sh.LastColumn];    
  22. int rowCount = xlRange.RowCount;    
  23. for (int i = 1; i < rowCount; i++)    
  24. {    
  25.         var col1 = xlRange.Columns[4].CellList[i].Value2.ToString();    
  26.         var col2 = xlRange.Columns[5].CellList[i].Value2.ToString();    
  27.         if (!dictionary2.ContainsKey(col1))    
  28.         {    
  29.                 dictionary2.Add(col1.ToString(), col2.ToString());    
  30.         }    
  31. }  
  32. //If ( the key value pair's of dictionary exist in dictionary2 )    
  33. {    
  34.         Console.WriteLine("Data already exists in database!!");    
  35. }    
  36. //Else    
  37. { Do some stuff }  
  38. wbk.Dispose();  
I think my above code approach is complicating things and not to mention unfinished.
 
Can anyone help me on this?
 
1st image newData.xlsx and 2nd image DATABASE.xlsx
 
 

Attachment: cSharp.zip

Answers (2)