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 {
param(
[Parameter(Mandatory)]
function ConvertFrom-ExcelSheet {
<#
.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,
[Parameter(Mandatory)]
[hashtable]$InfoMap,
[string]$Password,
[Switch]$Show,
[Switch]$AutoSize
[String]
$OutputPath = '.\',
[String]
$SheetName="*",
[string]
$Encoding = 'UTF8',
[string]
$Extension = '.txt',
[string]
$Delimiter = ';'
)
$parameters = @{}+$PSBoundParameters
$parameters.Remove("InfoMap")
$parameters.Remove("Show")
$Path = (Resolve-Path $Path).Path
$xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
$workbook = $xl.Workbook
$parameters.Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
$targetSheets = $workbook.Worksheets | Where {$_.Name -like $SheetName}
foreach ($entry in $InfoMap.GetEnumerator()) {
Write-Progress -Activity "Exporting" -Status "$($entry.Key)"
$parameters.WorkSheetname=$entry.Key
$params = @{} + $PSBoundParameters
$params.Remove("OutputPath")
$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}
}
}