Merge remote-tracking branch 'refs/remotes/origin/master' into dfinke/master

This commit is contained in:
DarkLite1
2017-02-23 08:59:39 +01:00
2 changed files with 252 additions and 221 deletions

View File

@@ -1,36 +1,43 @@
function Export-Excel { Function Export-Excel {
<# <#
.SYNOPSIS .SYNOPSIS
Export data to an Excel work sheet. Export data to an Excel work sheet.
.EXAMPLE .EXAMPLE
gsv | Export-Excel .\test.xlsx Get-Service | Export-Excel .\test.xlsx
.EXAMPLE .EXAMPLE
ps | Export-Excel .\test.xlsx -show\ Get-Process | Export-Excel .\test.xlsx -show\
.EXAMPLE .EXAMPLE
ps | Export-Excel .\test.xlsx -WorkSheetname Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM Get-Process | Export-Excel .\test.xlsx -WorkSheetname Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM
.EXAMPLE .EXAMPLE
ps | Export-Excel .\test.xlsx -WorkSheetname Processes -ChartType PieExploded3D -IncludePivotChart -IncludePivotTable -Show -PivotRows Company -PivotData PM Get-Process | Export-Excel .\test.xlsx -WorkSheetname Processes -ChartType PieExploded3D -IncludePivotChart -IncludePivotTable -Show -PivotRows Company -PivotData PM
.EXAMPLE .EXAMPLE
Remove-Item "c:\temp\test.xlsx" -ErrorAction Ignore Remove-Item "c:\temp\test.xlsx" -ErrorAction Ignore
Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'} Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'}
#> #>
param(
[CmdLetBinding()]
Param(
#[Parameter(Mandatory=$true)] #[Parameter(Mandatory=$true)]
$Path, $Path,
[Parameter(ValueFromPipeline=$true)] [Parameter(ValueFromPipeline=$true)]
$TargetData, $TargetData,
[string]$WorkSheetname="Sheet1", [String]$WorkSheetname='Sheet1',
[string]$Title, [String]$Title,
[OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern="None", [OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern='None',
[Switch]$TitleBold, [Switch]$TitleBold,
[int]$TitleSize=22, [Int]$TitleSize=22,
[System.Drawing.Color]$TitleBackgroundColor, [System.Drawing.Color]$TitleBackgroundColor,
[string[]]$PivotRows, [String[]]$PivotRows,
[string[]]$PivotColumns, [String[]]$PivotColumns,
$PivotData, $PivotData,
[Switch]$PivotDataToColumn, [Switch]$PivotDataToColumn,
[string]$Password, [String]$Password,
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Pie", [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType='Pie',
[Switch]$IncludePivotTable, [Switch]$IncludePivotTable,
[Switch]$IncludePivotChart, [Switch]$IncludePivotChart,
[Switch]$NoLegend, [Switch]$NoLegend,
@@ -42,24 +49,38 @@ function Export-Excel {
[Switch]$FreezeTopRow, [Switch]$FreezeTopRow,
[Switch]$FreezeFirstColumn, [Switch]$FreezeFirstColumn,
[Switch]$FreezeTopRowFirstColumn, [Switch]$FreezeTopRowFirstColumn,
[int[]]$FreezePane, [Int[]]$FreezePane,
[Switch]$AutoFilter, [Switch]$AutoFilter,
[Switch]$BoldTopRow, [Switch]$BoldTopRow,
[Switch]$NoHeader, [Switch]$NoHeader,
[string]$RangeName, [String]$RangeName,
[string]$TableName, [ValidateScript({
[OfficeOpenXml.Table.TableStyles]$TableStyle="Medium6", if ($_.Contains(' ')) {
throw 'Tablename has spaces.'
}
elseif (-not $_) {
throw 'Tablename is null or empty.'
}
elseif ($_[0] -notmatch '[a-z]') {
throw 'Tablename start with invalid character.'
}
else {
$true
}
})]
[String]$TableName,
[OfficeOpenXml.Table.TableStyles]$TableStyle='Medium6',
[Object[]]$ConditionalFormat, [Object[]]$ConditionalFormat,
[Object[]]$ConditionalText, [Object[]]$ConditionalText,
[Object[]]$ExcelChartDefinition, [Object[]]$ExcelChartDefinition,
[ScriptBlock]$CellStyleSB, [ScriptBlock]$CellStyleSB,
[string[]]$HideSheet, [String[]]$HideSheet,
[Switch]$KillExcel, [Switch]$KillExcel,
[Switch]$AutoNameRange, [Switch]$AutoNameRange,
$StartRow=1, $StartRow=1,
$StartColumn=1, $StartColumn=1,
[Switch]$PassThru, [Switch]$PassThru,
[string]$Numberformat="General", [String]$Numberformat='General',
[Switch]$Now [Switch]$Now
) )
@@ -70,9 +91,9 @@ function Export-Excel {
while (Get-Process excel -ErrorAction Ignore) {} while (Get-Process excel -ErrorAction Ignore) {}
} }
try { Try {
if ($Now) { if ($Now) {
$Path=[System.IO.Path]::GetTempFileName() -replace "\.tmp",".xlsx" $Path=[System.IO.Path]::GetTempFileName() -replace '\.tmp','.xlsx'
$Show=$true $Show=$true
$AutoSize=$true $AutoSize=$true
$AutoFilter=$true $AutoFilter=$true
@@ -94,7 +115,7 @@ function Export-Excel {
} }
# Force at least one cell value # Force at least one cell value
#$ws.Cells[1, 1].Value = "" #$ws.Cells[1, 1].Value = ''
$Row = $StartRow $Row = $StartRow
@@ -110,7 +131,7 @@ function Export-Excel {
$ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
#can only set TitleBackgroundColor if TitleFillPattern is something other than None #can only set TitleBackgroundColor if TitleFillPattern is something other than None
if($TitleBackgroundColor -AND ($TitleFillPattern -ne "None")) { if ($TitleBackgroundColor -AND ($TitleFillPattern -ne 'None')) {
$ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) $ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
} }
else { else {
@@ -120,7 +141,8 @@ function Export-Excel {
$Row += 1 $Row += 1
} }
} Catch { }
Catch {
if ($AlreadyExists) { if ($AlreadyExists) {
throw "$WorkSheetname already exists." throw "$WorkSheetname already exists."
} else { } else {
@@ -137,6 +159,7 @@ function Export-Excel {
if ($firstTimeThru) { if ($firstTimeThru) {
$firstTimeThru=$false $firstTimeThru=$false
$isDataTypeValueType = $TargetData.GetType().name -match "string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort" $isDataTypeValueType = $TargetData.GetType().name -match "string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort"
Write-Verbose "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType $isDataTypeValueType"
} }
if ($isDataTypeValueType) { if ($isDataTypeValueType) {
@@ -146,7 +169,7 @@ function Export-Excel {
$r=$null $r=$null
$cellValue=$TargetData $cellValue=$TargetData
if([Double]::TryParse([string]$cellValue,[System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::InvariantInfo, [ref]$r)) { if ([Double]::TryParse([string]$cellValue,[System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::CurrentInfo, [ref]$r)) {
$targetCell.Value = $r $targetCell.Value = $r
$targetCell.Style.Numberformat.Format=$Numberformat $targetCell.Style.Numberformat.Format=$Numberformat
} else { } else {
@@ -172,7 +195,8 @@ function Export-Excel {
$Row -= 1 $Row -= 1
} else { } else {
foreach ($Name in $script:Header) { foreach ($Name in $script:Header) {
$ws.Cells[$Row, $ColumnIndex].Value = $name Write-Verbose "Add header '$Name'"
$ws.Cells[$Row, $ColumnIndex].Value = $Name
$ColumnIndex += 1 $ColumnIndex += 1
} }
} }
@@ -182,7 +206,6 @@ function Export-Excel {
$ColumnIndex = $StartColumn $ColumnIndex = $StartColumn
foreach ($Name in $script:Header) { foreach ($Name in $script:Header) {
$targetCell = $ws.Cells[$Row, $ColumnIndex] $targetCell = $ws.Cells[$Row, $ColumnIndex]
$cellValue=$TargetData.$Name $cellValue=$TargetData.$Name
@@ -190,18 +213,21 @@ function Export-Excel {
if ($cellValue -is [string] -and $cellValue.StartsWith('=')) { if ($cellValue -is [string] -and $cellValue.StartsWith('=')) {
$targetCell.Formula = $cellValue $targetCell.Formula = $cellValue
} else { } else {
$r=$null $r=$null
if([Double]::TryParse([string]$cellValue,[System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::InvariantInfo, [ref]$r)) { if ([Double]::TryParse([string]$cellValue,[System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::CurrentInfo, [ref]$r)) {
$targetCell.Value = $r $targetCell.Value = $r
$targetCell.Style.Numberformat.Format=$Numberformat $targetCell.Style.Numberformat.Format=$Numberformat
Write-Verbose "Add cell value '$r' in Numberformat '$Numberformat'"
} else { } else {
$targetCell.Value = $cellValue $targetCell.Value = $cellValue
Write-Verbose "Add cell value '$cellValue' as String"
} }
} }
switch ($TargetData.$Name) { switch ($TargetData.$Name) {
{$_ -is [datetime]} {$targetCell.Style.Numberformat.Format = "m/d/yy h:mm"} {$_ -is [datetime]} {
$targetCell.Style.Numberformat.Format = "m/d/yy h:mm"
}
} }
#[ref]$uriResult=$null #[ref]$uriResult=$null
@@ -225,6 +251,7 @@ function Export-Excel {
} }
End { End {
Try {
if ($AutoNameRange) { if ($AutoNameRange) {
$totalRows=$ws.Dimension.Rows $totalRows=$ws.Dimension.Rows
$totalColumns=$ws.Dimension.Columns $totalColumns=$ws.Dimension.Columns
@@ -350,7 +377,6 @@ function Export-Excel {
foreach ($Sheet in $HideSheet) { foreach ($Sheet in $HideSheet) {
$pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden" $pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden"
} }
$chartCount=0 $chartCount=0
@@ -434,4 +460,8 @@ function Export-Excel {
if ($Show) {Invoke-Item $Path} if ($Show) {Invoke-Item $Path}
} }
} }
Catch {
throw "Failed exporting the worksheet: $_"
}
}
} }

View File

@@ -192,6 +192,7 @@ function Add-WorkSheet {
$ws = $ExcelPackage.Workbook.Worksheets[$WorkSheetname] $ws = $ExcelPackage.Workbook.Worksheets[$WorkSheetname]
if(!$ws) { if(!$ws) {
Write-Verbose "Add worksheet '$WorkSheetname'"
$ws=$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname) $ws=$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname)
} }