[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutFile = '', [string]$WorksheetName = 'Data', [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Get-Command Export-Excel -ErrorAction SilentlyContinue)) { throw "Модуль ImportExcel не установлен. Выполните: Install-Module ImportExcel -Scope CurrentUser -Force" } if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Файл не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).Path if ([string]::IsNullOrWhiteSpace($OutFile)) { $OutFile = [System.IO.Path]::ChangeExtension($resolved, '.xlsx') } if ([System.IO.Path]::IsPathRooted($OutFile)) { $outFull = [System.IO.Path]::GetFullPath($OutFile) } else { $outFull = [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $OutFile)) } if ($resolved -eq $outFull) { throw "Выходной файл не должен совпадать с исходным" } if (Test-Path -LiteralPath $outFull) { if (-not $Force) { throw "Файл уже существует: $outFull. Добавьте -Force для перезаписи" } } $rows = @(Import-Csv -LiteralPath $resolved) if ($rows.Count -eq 0) { throw "В CSV нет строк данных" } $rows | Export-Excel -Path $outFull -WorksheetName $WorksheetName -AutoSize -AutoFilter -FreezeTopRow -BoldTopRow -ClearSheet Write-Host "Строк: $($rows.Count)" Write-Host "Колонок: $($rows[0].PSObject.Properties.Name.Count)" Write-Host "Результат: $outFull"