[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$CsvPath ) $ErrorActionPreference = 'Stop' $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path $extension = [IO.Path]::GetExtension($resolved).ToLowerInvariant() if ($extension -notin @('.xlsx', '.xlsm', '.xls')) { throw 'Поддерживаются файлы .xlsx, .xlsm и .xls' } $excel = $null $workbook = $null try { try { $excel = New-Object -ComObject Excel.Application } catch { throw 'Excel не найден. Установите настольный Microsoft Excel.' } $excel.Visible = $false $excel.DisplayAlerts = $false $workbook = $excel.Workbooks.Open($resolved, 0, $true) $rows = New-Object System.Collections.Generic.List[object] foreach ($sheet in $workbook.Worksheets) { try { $cells = $sheet.UsedRange.SpecialCells(-4123) } catch { continue } foreach ($cell in $cells) { $value = '' try { if ($null -ne $cell.Value2) { $value = [string]$cell.Value2 } } catch { $value = '#VALUE!' } $rows.Add([pscustomobject]@{ Sheet = $sheet.Name Address = $cell.Address($false, $false) Formula = [string]$cell.Formula Value = $value }) } } if ($rows.Count -eq 0) { Write-Host 'Формулы не найдены.' } else { $rows | Format-Table -AutoSize } if ($CsvPath) { $rows | Export-Csv -LiteralPath $CsvPath -NoTypeInformation -Encoding UTF8 Write-Host "Отчёт сохранён: $CsvPath" } } finally { if ($workbook) { $workbook.Close($false) } if ($excel) { $excel.Quit() } if ($workbook) { [void][Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) } if ($excel) { [void][Runtime.InteropServices.Marshal]::ReleaseComObject($excel) } [GC]::Collect() }