Updated to ConvertFrom-ExcelSheet

This commit is contained in:
Doug Finke
2015-04-20 16:04:43 -04:00
parent b4e89b95d5
commit a0602f3361

View File

@@ -232,29 +232,55 @@ function Export-Excel {
} }
} }
function Export-MultipleExcelSheets { function ConvertFrom-ExcelSheet {
param( <#
[Parameter(Mandatory)] .Synopsis
Reads an Excel file an converts the data to a delimited text file
.Example
ConvertFrom-ExcelSheet .\TestSheets.xlsx .\data
Reads each sheet in TestSheets.xlsx and outputs it to the data directory as the sheet name with the extension .txt
.Example
ConvertFrom-ExcelSheet .\TestSheets.xlsx .\data sheet?0
Reads and outputs sheets like Sheet10 and Sheet20 form TestSheets.xlsx and outputs it to the data directory as the sheet name with the extension .txt
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$Path, $Path,
[Parameter(Mandatory)] [String]
[hashtable]$InfoMap, $OutputPath = '.\',
[string]$Password, [String]
[Switch]$Show, $SheetName="*",
[Switch]$AutoSize [string]
$Encoding = 'UTF8',
[string]
$Extension = '.txt',
[string]
$Delimiter = ';'
) )
$parameters = @{}+$PSBoundParameters $Path = (Resolve-Path $Path).Path
$parameters.Remove("InfoMap") $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
$parameters.Remove("Show") $workbook = $xl.Workbook
$parameters.Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $targetSheets = $workbook.Worksheets | Where {$_.Name -like $SheetName}
foreach ($entry in $InfoMap.GetEnumerator()) { $params = @{} + $PSBoundParameters
Write-Progress -Activity "Exporting" -Status "$($entry.Key)" $params.Remove("OutputPath")
$parameters.WorkSheetname=$entry.Key $params.Remove("SheetName")
$params.NoTypeInformation = $true
& $entry.Value | Export-Excel @parameters 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
} }
if($Show) {Invoke-Item $Path}
} }