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
Export data to an Excel work sheet.
.EXAMPLE
gsv | Export-Excel .\test.xlsx
Get-Service | Export-Excel .\test.xlsx
.EXAMPLE
ps | Export-Excel .\test.xlsx -show\
Get-Process | Export-Excel .\test.xlsx -show\
.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
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
Remove-Item "c:\temp\test.xlsx" -ErrorAction Ignore
Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'}
#>
param(
[CmdLetBinding()]
Param(
#[Parameter(Mandatory=$true)]
$Path,
[Parameter(ValueFromPipeline=$true)]
$TargetData,
[string]$WorkSheetname="Sheet1",
[string]$Title,
[OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern="None",
[String]$WorkSheetname='Sheet1',
[String]$Title,
[OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern='None',
[Switch]$TitleBold,
[int]$TitleSize=22,
[Int]$TitleSize=22,
[System.Drawing.Color]$TitleBackgroundColor,
[string[]]$PivotRows,
[string[]]$PivotColumns,
[String[]]$PivotRows,
[String[]]$PivotColumns,
$PivotData,
[Switch]$PivotDataToColumn,
[string]$Password,
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Pie",
[String]$Password,
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType='Pie',
[Switch]$IncludePivotTable,
[Switch]$IncludePivotChart,
[Switch]$NoLegend,
@@ -42,24 +49,38 @@ function Export-Excel {
[Switch]$FreezeTopRow,
[Switch]$FreezeFirstColumn,
[Switch]$FreezeTopRowFirstColumn,
[int[]]$FreezePane,
[Int[]]$FreezePane,
[Switch]$AutoFilter,
[Switch]$BoldTopRow,
[Switch]$NoHeader,
[string]$RangeName,
[string]$TableName,
[OfficeOpenXml.Table.TableStyles]$TableStyle="Medium6",
[String]$RangeName,
[ValidateScript({
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[]]$ConditionalText,
[Object[]]$ExcelChartDefinition,
[ScriptBlock]$CellStyleSB,
[string[]]$HideSheet,
[String[]]$HideSheet,
[Switch]$KillExcel,
[Switch]$AutoNameRange,
$StartRow=1,
$StartColumn=1,
[Switch]$PassThru,
[string]$Numberformat="General",
[String]$Numberformat='General',
[Switch]$Now
)
@@ -70,9 +91,9 @@ function Export-Excel {
while (Get-Process excel -ErrorAction Ignore) {}
}
try {
Try {
if ($Now) {
$Path=[System.IO.Path]::GetTempFileName() -replace "\.tmp",".xlsx"
$Path=[System.IO.Path]::GetTempFileName() -replace '\.tmp','.xlsx'
$Show=$true
$AutoSize=$true
$AutoFilter=$true
@@ -94,7 +115,7 @@ function Export-Excel {
}
# Force at least one cell value
#$ws.Cells[1, 1].Value = ""
#$ws.Cells[1, 1].Value = ''
$Row = $StartRow
@@ -110,7 +131,7 @@ function Export-Excel {
$ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
#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)
}
else {
@@ -120,7 +141,8 @@ function Export-Excel {
$Row += 1
}
} Catch {
}
Catch {
if ($AlreadyExists) {
throw "$WorkSheetname already exists."
} else {
@@ -137,6 +159,7 @@ function Export-Excel {
if ($firstTimeThru) {
$firstTimeThru=$false
$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) {
@@ -146,7 +169,7 @@ function Export-Excel {
$r=$null
$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.Style.Numberformat.Format=$Numberformat
} else {
@@ -172,7 +195,8 @@ function Export-Excel {
$Row -= 1
} else {
foreach ($Name in $script:Header) {
$ws.Cells[$Row, $ColumnIndex].Value = $name
Write-Verbose "Add header '$Name'"
$ws.Cells[$Row, $ColumnIndex].Value = $Name
$ColumnIndex += 1
}
}
@@ -182,7 +206,6 @@ function Export-Excel {
$ColumnIndex = $StartColumn
foreach ($Name in $script:Header) {
$targetCell = $ws.Cells[$Row, $ColumnIndex]
$cellValue=$TargetData.$Name
@@ -190,18 +213,21 @@ function Export-Excel {
if ($cellValue -is [string] -and $cellValue.StartsWith('=')) {
$targetCell.Formula = $cellValue
} else {
$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.Style.Numberformat.Format=$Numberformat
Write-Verbose "Add cell value '$r' in Numberformat '$Numberformat'"
} else {
$targetCell.Value = $cellValue
Write-Verbose "Add cell value '$cellValue' as String"
}
}
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
@@ -225,6 +251,7 @@ function Export-Excel {
}
End {
Try {
if ($AutoNameRange) {
$totalRows=$ws.Dimension.Rows
$totalColumns=$ws.Dimension.Columns
@@ -350,7 +377,6 @@ function Export-Excel {
foreach ($Sheet in $HideSheet) {
$pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden"
}
$chartCount=0
@@ -434,4 +460,8 @@ function Export-Excel {
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]
if(!$ws) {
Write-Verbose "Add worksheet '$WorkSheetname'"
$ws=$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname)
}