[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutDir = '', [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Get-Command Import-Excel -ErrorAction SilentlyContinue)) { throw "Модуль ImportExcel не установлен. Выполните: Install-Module ImportExcel -Scope CurrentUser -Force" } if (-not (Get-Command Get-ExcelSheetInfo -ErrorAction SilentlyContinue)) { throw "Команда Get-ExcelSheetInfo недоступна. Обновите модуль ImportExcel" } if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Файл не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).Path if ([string]::IsNullOrWhiteSpace($OutDir)) { $OutDir = Join-Path ([System.IO.Path]::GetDirectoryName($resolved)) ([System.IO.Path]::GetFileNameWithoutExtension($resolved) + '_csv') } if ([System.IO.Path]::IsPathRooted($OutDir)) { $outFull = [System.IO.Path]::GetFullPath($OutDir) } else { $outFull = [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $OutDir)) } if (-not (Test-Path -LiteralPath $outFull)) { New-Item -ItemType Directory -Path $outFull | Out-Null } $sheets = @(Get-ExcelSheetInfo -Path $resolved | Select-Object -ExpandProperty Name) if ($sheets.Count -eq 0) { throw "В книге не найдено листов" } $written = 0 foreach ($sheet in $sheets) { $safeName = $sheet -replace '[\\/:*?"<>|]', '_' $target = Join-Path $outFull ($safeName + '.csv') if (Test-Path -LiteralPath $target) { if (-not $Force) { throw "Файл уже существует: $target. Добавьте -Force" } } $data = @(Import-Excel -Path $resolved -WorksheetName $sheet) if ($data.Count -eq 0) { Set-Content -LiteralPath $target -Value '' -Encoding UTF8 Write-Warning "Лист пуст: $sheet" } else { $data | Export-Csv -LiteralPath $target -NoTypeInformation -Encoding UTF8 } $written += 1 Write-Host "Лист $sheet -> $target" } Write-Host "Листов обработано: $written" Write-Host "Папка: $outFull"