diff --git a/.gitignore b/.gitignore index 3060476..6fb93a2 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,5 @@ testPwd.xlsx test.csv EditPSM1.ps1 TryTitle.ps1 +data/Sheet10.txt +data/Sheet20.txt diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index a5e4970..465f881 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -43,37 +43,45 @@ function Import-Excel { } } -function Import-ExcelAllSheet -{ - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [String] - $Path, - [String] - $OutputPath = '.\', - [string] - $Encoding = 'UTF8', - [string] - $Extension = '.txt', - [string] - $Delimiter = ';' - ) - - $FullName = (Resolve-Path $Path).Path - $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $FullName - $workbook = $xl.Workbook - - - Foreach ($sheet in $workbook.Worksheets) - { - Write-Verbose "Exporting sheet: $($sheet.name)" - - Import-Excel -FullName $FullName -Sheet $($sheet.Name) | Export-Csv -Path "$($OutputPath)\$($Sheet.Name)$($Extension)" -Delimiter $Delimiter -NoTypeInformation -Encoding $Encoding - - } - +function Export-ExcelSheet { + + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $Path, + [String] + $OutputPath = '.\', + [String] + $SheetName, + [string] + $Encoding = 'UTF8', + [string] + $Extension = '.txt', + [string] + $Delimiter = ';' + ) + + $Path = (Resolve-Path $Path).Path + $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path + $workbook = $xl.Workbook + + $targetSheets = $workbook.Worksheets | Where {$_.Name -Match $SheetName} + + $params = @{} + $PSBoundParameters + $params.Remove("OutputPath") + $params.Remove("SheetName") + $params.NoTypeInformation = $true + + Foreach ($sheet in $targetSheets) + { + Write-Verbose "Exporting sheet: $($sheet.Name)" + + $params.Path = "$($OutputPath)\$($Sheet.Name)$($Extension)" + + Import-Excel $Path -Sheet $($sheet.Name) | Export-Csv @params + } } function Export-Excel { diff --git a/TestSheets.xlsx b/TestSheets.xlsx new file mode 100644 index 0000000..9b2fbc7 Binary files /dev/null and b/TestSheets.xlsx differ