As RichMatheisen said, It is way less complex to work with CSV files.
This was for the sake of trying something new. Now that I've done it, I'm sure I'll keep converting xlsx to csv.
It is possible to output in xlsx too, but I couldn't see any added value over csv
$codes = "F3H", "G9T", "CL3"
$ExcelObj = New-Object -ComObject Excel.Application
$ExcelObj.Visible = $false
$ExcelFile = $ExcelObj.Workbooks.Open("C:\samples\Inputs.xlsx")
$ExcelSheet = $ExcelFile.Sheets.Item("Sheet1")
$Inputs = foreach ($row in $ExcelSheet.UsedRange.Rows){
[PSCustomObject]@{
Location = $row.Value2.split(',')[0]
Code = $row.Value2.split(',')[1]
Size = $row.Value2.split(',')[2]
In = $row.Value2.split(',')[3].Trim('%')
}
}
$Outputs = foreach ($line in $Inputs){
$lookup = $line.Code
foreach ($code in $codes){
if ($lookup -like "*$code*"){
[PSCustomObject]@{
Location = $line.Location
Code = $line.Code
Size = $line.Size
Total = [System.Math]::Round($line.In).ToString() + "%"
}
}
}
}
$Outputs | Export-CSV "c:\samples\Outputs.csv" -NoTypeInformation