`I have below code to compare files inside zip folder. The code works fine for .csv, .xml. I have problem with .xlsx. The zip folder has .xlsx file which is system generated. By naked eye the content of excel look same but the Get-Content and Compare-Object identify them as different. The code works fine when I copy same file and compare. Any help is appreciated. Thanks
`# Function to compare the contents of two Excel files
function Compare-ExcelContent {
param (
[string]$excelFile1,
[string]$excelFile2
)
$excelApp = New-Object -ComObject Excel.Application
$workbook1 = $excelApp.Workbooks.Open($excelFile1)
$workbook2 = $excelApp.Workbooks.Open($excelFile2)
$sheet1 = $workbook1.Sheets.Item(1)
$sheet2 = $workbook2.Sheets.Item(1)
$content1 = $sheet1.UsedRange.Value2
$content2 = $sheet2.UsedRange.Value2
$workbook1.Close()
$workbook2.Close()
$excelApp.Quit()
return ($content1 -eq $content2)
}
# Function to compare the contents of files inside zip folders
$excelFiles1 = $zip1.Entries | Where-Object { $_.Name -like "*.xlsx" -or $_.Name -like "*.xls" }
$excelFiles2 = $zip2.Entries | Where-Object { $_.Name -like "*.xlsx" -or $_.Name -like "*.xls" }
foreach ($excel1 in $excelFiles1) {
foreach ($excel2 in $excelFiles2) {
$stream1 = $excel1.Open()
$stream2 = $excel2.Open()
$reader1 = New-Object System.IO.StreamReader($stream1)
$reader2 = New-Object System.IO.StreamReader($stream2)
$content1 = $reader1.ReadToEnd()
$content2 = $reader2.ReadToEnd()
if ($content1 -eq $content2) {
Write-Output "$($excel1.Name) and $($excel2.Name) are identical."
}
Else {Write-Output "$($excel1.Name) and $($excel2.Name) are different."}
$reader1.Close()
$reader2.Close()
$stream1.Close()
$stream2.Close()
}
}
# Path
$zipFile1 = "Address 1"
$zipFile2 = "Address 2"
Compare-ZipContents -zipFile1 $zipFile1 -zipFile2 $zipFile2
New contributor
Amitabh Srivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.