Implemented -WorkSheetname parameter

This commit is contained in:
Doug Finke
2015-04-08 18:20:10 -04:00
parent 293764478b
commit eb8fbc4f4a

View File

@@ -49,6 +49,7 @@ function Export-Excel {
$Path, $Path,
[Parameter(ValueFromPipeline)] [Parameter(ValueFromPipeline)]
$TargetData, $TargetData,
$WorkSheetname="Sheet1",
[string[]]$PivotRows, [string[]]$PivotRows,
[string[]]$PivotColumns, [string[]]$PivotColumns,
[string[]]$PivotData, [string[]]$PivotData,
@@ -61,20 +62,19 @@ function Export-Excel {
) )
Begin { Begin {
try {
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
if(Test-Path $Path) {
if($Force) {
Remove-Item $Path
} else {
throw "$Path already exists"
}
}
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path $pkg = New-Object OfficeOpenXml.ExcelPackage $Path
$ws = $pkg.Workbook.Worksheets.Add("Sheet1")
if($pkg.Workbook.Worksheets[$WorkSheetname]) {
$pkg.Workbook.Worksheets.delete($WorkSheetname)
}
$ws = $pkg.Workbook.Worksheets.Add($WorkSheetname)
$Row = 1 $Row = 1
} Catch {
throw $Error[0].Exception.InnerException
}
} }
Process { Process {
@@ -101,15 +101,16 @@ function Export-Excel {
End { End {
if($AutoFitColumns) { $ws.Cells.AutoFitColumns()} if($AutoFitColumns) {$ws.Cells.AutoFitColumns()}
if($IncludePivotTable) { if($IncludePivotTable) {
$pivotTableName = $WorkSheetname + "PivotTable"
$wsPivot = $pkg.Workbook.Worksheets.Add($pivotTableName)
#$wsPivot.View.TabSelected = $true
$wsPivot = $pkg.Workbook.Worksheets.Add("PivotTable1") $pivotTableDataName=$WorkSheetname + "PivotTableData"
$wsPivot.View.TabSelected = $true
$range="{0}:{1}" -f $ws.Dimension.Start.Address, $ws.Dimension.End.Address $range="{0}:{1}" -f $ws.Dimension.Start.Address, $ws.Dimension.End.Address
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$range], "PivotTableData") $pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$range], $pivotTableDataName)
if($PivotRows) { if($PivotRows) {
foreach ($Row in $PivotRows) { foreach ($Row in $PivotRows) {
@@ -130,14 +131,13 @@ function Export-Excel {
} }
if($IncludePivotChart) { if($IncludePivotChart) {
#$ChartType="Pie"
#$ChartType="PieExploded3D"
$chart = $wsPivot.Drawings.AddChart("PivotChart", $ChartType, $pivotTable) $chart = $wsPivot.Drawings.AddChart("PivotChart", $ChartType, $pivotTable)
$chart.SetPosition(1, 0, 6, 0) $chart.SetPosition(1, 0, 6, 0)
$chart.SetSize(600, 400) $chart.SetSize(600, 400)
} }
} }
$pkg.Save() $pkg.Save()
$pkg.Dispose() $pkg.Dispose()