[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 = foreach ($sheet in $workbook.Worksheets) { $range = $sheet.UsedRange [pscustomobject]@{ Workbook = [IO.Path]::GetFileName($resolved) Sheet = $sheet.Name Visible = [string]$sheet.Visible Address = $range.Address($false, $false) Rows = $range.Rows.Count Columns = $range.Columns.Count UsedCells = $range.Count } } $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() }