Hi ,
You can use this function
function CompareExcel(ExpectedFileName, ActualFileName, RegExpr: OleVariant = nil, CompareProperty: string = 'Text'): boolean;
var
excel,
expectedFile, actualFile,
expectedSheet, actualSheet,
regEx: OleVariant;
s, expectedStr, actualStr: string;
col, row, i, j: integer;
begin
try
result := true;
//check expected file exists
if not aqFile.Exists(ExpectedFileName) then
begin
result := false;
Log.Warning('Expected file not exists.', ExpectedFileName);
exit;
end;
//check actual file exists
if not aqFile.Exists(ActualFileName) then
begin
result := false;
Log.Error('Actual file not exists.', ActualFileName);
exit;
end;
//open files
excel := Sys.OleObject['Excel.Application'];
expectedFile := excel.WorkBooks.Open(ExpectedFileName);
actualFile := excel.WorkBooks.Open(ActualFileName);
//get sheets
expectedSheet := expectedFile.WorkSheets[1].UsedRange;
actualSheet := actualFile.WorkSheets[1].UsedRange;
//compare no of columns
col := expectedSheet.Columns.Count; //will hold the highest col
if expectedSheet.Columns.Count <> actualSheet.Columns.Count then
begin
s := 'Expected: ' + VarToStr(expectedSheet.Columns.Count) + #13#10 +
'Actual: ' + VarToStr(actualSheet.Columns.Count);
Log.Error('Excel no of columns are not identical.', s);
result := false;
//exit;
if expectedSheet.Columns.Count < actualSheet.Columns.Count then
col := actualSheet.Columns.Count;
end;
//compare no of rows
row := expectedSheet.Rows.Count; //will hold the highest row
if expectedSheet.Rows.Count <> actualSheet.Rows.Count then
begin
s := 'Expected: ' + VarToStr(expectedSheet.Rows.Count) + #13#10 +
'Actual: ' + VarToStr(actualSheet.Rows.Count);
Log.Error('Excel no of rows are not identical.', s);
result := false;
//exit;
if expectedSheet.Rows.Count < actualSheet.Rows.Count then
row := actualSheet.Rows.Count;
end;
if VarType(regExpr) > 3 then
begin
//Creates the regular expression object
regEx := HISUtils.RegExpr;
regEx.Expression := regExpr;
end;
for i := 1 to row do //for each row
begin
for j := 1 to col do //for each column
begin
expectedStr := aqString.Trim(VarToStr(Evaluate('expectedSheet.Rows[i].Columns[j].' + CompareProperty)));
actualStr := aqString.Trim(VarToStr(Evaluate('actualSheet.Rows[i].Columns[j].' + CompareProperty)));
if VarType(regExpr) > 3 then //apply reg expr if it is defined, vartype(nil) = 3
begin
//Replaces the text matching the expression with <ignore>
expectedStr := regEx.Replace(expectedStr, '<ignore>');
actualStr := regEx.Replace(actualStr, '<ignore>');
end;
// If expected string is 'SUN' (the super-user for Sun4 systems) AND this is a Sun5 test, change it to 'TTT'
if (expectedstr = 'SUN') then
expectedstr := GetUserIDs('0');
if not Utilities.SameText(expectedStr, actualStr) then
begin
Log.Error('Cell [' + VarToStr(i) + ',' + VarToStr(j) + '] : Expected - ' + VarToStr(expectedStr) + ' | Actual - ' + VarToStr(actualStr));
result := false;
//exit;
end;
end;
end;
if result then
Log.Message('Files are identical,click on Addtional Information tab for details... ',' Expected File : ' + #13#10 + ExpectedFileName + #13#10 + 'Actual File : '+#13#10+ ActualFileName)
else
Log.Error('Files are not identical,click on Addtional Information tab for details... ',' Expected File : ' + #13#10 + ExpectedFileName + #13#10 + 'Actual File : '+#13#10+ ActualFileName);
finally
//close files
expectedFile.Close;
actualFile.Close;
end;
end;