diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index cc305b3..6a8ce76 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -41,4 +41,105 @@ function Import-Excel { $xl.Dispose() $xl = $null } +} + +function Export-Excel { + param( + [Parameter(Mandatory)] + $Path, + [Parameter(ValueFromPipeline)] + $TargetData, + [string[]]$PivotRows, + [string[]]$PivotColumns, + [string[]]$PivotData, + [ValidateSet("Area3D","AreaStacked3D","AreaStacked1003D","BarClustered3D","BarStacked3D","BarStacked1003D","Column3D","ColumnClustered3D","ColumnStacked3D","ColumnStacked1003D","Line3D","Pie3D","PieExploded3D","Area","AreaStacked","AreaStacked100","BarClustered","BarOfPie","BarStacked","BarStacked100","Bubble","Bubble3DEffect","ColumnClustered","ColumnStacked","ColumnStacked100","ConeBarClustered","ConeBarStacked","ConeBarStacked100","ConeCol","ConeColClustered","ConeColStacked","ConeColStacked100","CylinderBarClustered","CylinderBarStacked","CylinderBarStacked100","CylinderCol","CylinderColClustered","CylinderColStacked","CylinderColStacked100","Doughnut","DoughnutExploded","Line","LineMarkers","LineMarkersStacked","LineMarkersStacked100","LineStacked","LineStacked100","Pie","PieExploded","PieOfPie","PyramidBarClustered","PyramidBarStacked","PyramidBarStacked100","PyramidCol","PyramidColClustered","PyramidColStacked","PyramidColStacked100","Radar","RadarFilled","RadarMarkers","StockHLC","StockOHLC","StockVHLC","StockVOHLC","Surface","SurfaceTopView","SurfaceTopViewWireframe","SurfaceWireframe","XYScatter","XYScatterLines","XYScatterLinesNoMarkers","XYScatterSmooth","XYScatterSmoothNoMarkers")] + $ChartType="Pie", + [Switch]$IncludePivotTable, + [Switch]$IncludePivotChart, + [Switch]$AutoFitColumns, + [Switch]$Show, + [Switch]$Force + ) + + Begin { + + if(Test-Path $Path) { + if($Force) { + Remove-Item $Path + } else { + throw "$Path already exists" + } + } + + $pkg = New-Object OfficeOpenXml.ExcelPackage $Path + $ws = $pkg.Workbook.Worksheets.Add("Sheet1") + $Row = 1 + } + + Process { + + if(!$Header) { + + $ColumnIndex = 1 + $Header = $TargetData.psobject.properties.name + + foreach ($Name in $Header) { + $ws.Cells[$Row, $ColumnIndex].Value = $name + $ColumnIndex += 1 + } + } + + $Row += 1 + $ColumnIndex = 1 + + foreach ($Name in $Header) { + $ws.Cells[$Row, $ColumnIndex].Value = $TargetData.$Name + $ColumnIndex += 1 + } + } + + End { + + if($AutoFitColumns) { $ws.Cells.AutoFitColumns()} + + if($IncludePivotTable) { + + $wsPivot = $pkg.Workbook.Worksheets.Add("PivotTable1") + $wsPivot.View.TabSelected = $true + + $range="{0}:{1}" -f $ws.Dimension.Start.Address, $ws.Dimension.End.Address + $pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells["A1"], $ws.Cells[$range], "PivotTableData") + + if($PivotRows) { + foreach ($Row in $PivotRows) { + $null=$pivotTable.RowFields.Add($pivotTable.Fields[$Row]) + } + } + + if($PivotColumns) { + foreach ($Column in $PivotColumns) { + $null=$pivotTable.ColumnFields.Add($pivotTable.Fields[$Column]) + } + } + + if($PivotData) { + foreach ($Item in $PivotData) { + $null=$pivotTable.DataFields.Add($pivotTable.Fields[$Item]) + } + } + + if($IncludePivotChart) { + #$ChartType="Pie" + #$ChartType="PieExploded3D" + $chart = $wsPivot.Drawings.AddChart("PivotChart", $ChartType, $pivotTable) + $chart.SetPosition(1, 0, 6, 0) + $chart.SetSize(600, 400) + } + } + + $pkg.Save() + $pkg.Dispose() + + if($Show) {Invoke-Item $Path} + } } \ No newline at end of file diff --git a/README.md b/README.md index 50a67ff..d88c09f 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ Get-Process Exported to Excel ### PowerShell Excel EPPlus In Action - +Click on this image to watch the short video. + +[![image](http://dougfinke.com/powershellvideos/ExportExcel/ExportExcel_First_Frame.png)](http://dougfinke.com/powershellvideos/ExportExcel/ExportExcel.html) ### Importing data from an Excel spreadsheet