# Convert each sheet of an .xlsb workbook to CSV using Excel COM, with macros force-disabled. param([string]$InFile, [string]$OutDir) New-Item -ItemType Directory -Force $OutDir | Out-Null $OutDir = (Resolve-Path $OutDir).Path $xl = New-Object -ComObject Excel.Application $xl.Visible = $false $xl.DisplayAlerts = $false $xl.AutomationSecurity = 3 # msoAutomationSecurityForceDisable try { $wb = $xl.Workbooks.Open((Resolve-Path $InFile).Path, 0, $true) $n = $wb.Worksheets.Count $names = @(); for ($i = 1; $i -le $n; $i++) { $names += $wb.Worksheets.Item($i).Name } for ($i = 1; $i -le $n; $i++) { $ws = $wb.Worksheets.Item($i) $ws.Activate() $name = $names[$i-1] -replace '[^A-Za-z0-9_-]', '_' $out = Join-Path $OutDir ("$name.csv") $wb.SaveAs($out, 62) # xlCSVUTF8 saves active sheet Write-Output "wrote $out" } $wb.Close($false) } finally { $xl.Quit() [System.Runtime.InteropServices.Marshal]::ReleaseComObject($xl) | Out-Null }