diff --git a/Examples/notes.md b/Examples/notes.md new file mode 100644 index 0000000..e69de29 diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index ee757eb..6693c06 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -40,11 +40,22 @@ function Export-Excel { [Switch]$NoHeader, [string]$RangeName, [string]$TableName, + [OfficeOpenXml.Table.TableStyles]$TableStyle="Medium6", [Object[]]$ConditionalFormat, - [string[]]$HideSheet + [Object[]]$ExcelChartDefinition, + [string[]]$HideSheet, + [Switch]$KillExcel, + [Switch]$AutoNameRange, + $StartRow=1, + $StartColumn=1 ) Begin { + if($KillExcel) { + Get-Process excel -ErrorAction Ignore | Stop-Process + while (Get-Process excel -ErrorAction Ignore) {} + } + try { $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) if (Test-Path $path) { @@ -56,25 +67,26 @@ function Export-Excel { foreach($format in $ConditionalFormat ) { $target = "Add$($format.Formatter)" - $rule = ($ws.ConditionalFormatting).$target.Invoke($format.Address, $format.IconType) + $rule = ($ws.ConditionalFormatting).$target($format.Address, $format.IconType) $rule.Reverse = $format.Reverse } # Force at least one cell value - $ws.Cells[1, 1].Value = "" + #$ws.Cells[1, 1].Value = "" - $Row = 1 + + $Row = $StartRow if($Title) { - $ws.Cells[$Row, 1].Value = $Title + $ws.Cells[$Row, $StartColumn].Value = $Title - $ws.Cells[$Row, 1].Style.Font.Size = $TitleSize - $ws.Cells[$Row, 1].Style.Font.Bold = $TitleBold - $ws.Cells[$Row, 1].Style.Fill.PatternType = $TitleFillPattern + $ws.Cells[$Row, $StartColumn].Style.Font.Size = $TitleSize + $ws.Cells[$Row, $StartColumn].Style.Font.Bold = $TitleBold + $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern if($TitleBackgroundColor) { - $ws.Cells[$Row, 1].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) + $ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor) } - $Row = 2 + $Row += 1 } } Catch { @@ -97,7 +109,7 @@ function Export-Excel { } if($isDataTypeValueType) { - $ColumnIndex = 1 + $ColumnIndex = $StartColumn $targetCell = $ws.Cells[$Row, $ColumnIndex] @@ -119,7 +131,8 @@ function Export-Excel { } else { if(!$Header) { - $ColumnIndex = 1 + $ColumnIndex = $StartColumn + $Header = $TargetData.psobject.properties.name if($NoHeader) { @@ -134,7 +147,7 @@ function Export-Excel { } $Row += 1 - $ColumnIndex = 1 + $ColumnIndex = $StartColumn foreach ($Name in $Header) { @@ -142,11 +155,16 @@ function Export-Excel { $cellValue=$TargetData.$Name - $r=$null - if([double]::tryparse($cellValue, [ref]$r)) { - $targetCell.Value = $r + if($cellValue -is [string] -and $cellValue.StartsWith('=')) { + $targetCell.Formula = $cellValue } else { - $targetCell.Value = $cellValue + + $r=$null + if([double]::tryparse($cellValue, [ref]$r)) { + $targetCell.Value = $r + } else { + $targetCell.Value = $cellValue + } } switch ($TargetData.$Name) { @@ -159,15 +177,43 @@ function Export-Excel { } End { + if($AutoNameRange) { + $totalRows=$ws.Dimension.Rows + $totalColumns=$ws.Dimension.Columns + + foreach($c in 0..($totalColumns-1)) { + $targetRangeName = "$($Header[$c])" + $targetColumn = $c+1 + $theCell = $ws.Cells[2,$targetColumn,$totalRows,$targetColumn ] + $ws.Names.Add($targetRangeName, $theCell) | Out-Null + } + } + $startAddress=$ws.Dimension.Start.Address $dataRange="{0}:{1}" -f $startAddress, $ws.Dimension.End.Address + Write-Debug "Data Range $dataRange" if (-not [string]::IsNullOrEmpty($RangeName)) { $ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null } + if (-not [string]::IsNullOrEmpty($TableName)) { - $ws.Tables.Add($ws.Cells[$dataRange], $TableName) | Out-Null + #$ws.Tables.Add($ws.Cells[$dataRange], $TableName) | Out-Null + #"$($StartRow),$($StartColumn),$($ws.Dimension.End.Row-$StartRow),$($Header.Count)" + + $csr=$StartRow + $csc=$StartColumn + $cer=$ws.Dimension.End.Row #-$StartRow+1 + $cec=$Header.Count + + $targetRange=$ws.Cells[$csr, $csc, $cer,$cec] + + $tbl = $ws.Tables.Add($targetRange, $TableName) + + $tbl.TableStyle=$TableStyle + + $idx } if($IncludePivotTable) { @@ -235,6 +281,27 @@ function Export-Excel { foreach($Sheet in $HideSheet) { $pkg.Workbook.WorkSheets[$Sheet].Hidden="Hidden" + + } + + $chartCount=0 + foreach ($chartDef in $ExcelChartDefinition) { + $ChartName = "Chart"+(Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.','' + $chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType) + $chart.Title.Text = $chartDef.Title + $chart.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels,$chartDef.Column, $chartDef.ColumnOffsetPixels) + $chart.SetSize($chartDef.Width, $chartDef.Height) + + $chartDefCount = @($chartDef.XRange).Count + if($chartDefCount -eq 1) { + $Series=$chart.Series.Add($chartDef.YRange, $chartDef.XRange) + $Series.Header = $chartDef.Header + } else { + for($idx=0; $idx -lt $chartDefCount; $idx+=1) { + $Series=$chart.Series.Add($chartDef.YRange[$idx], $chartDef.XRange) + $Series.Header = $chartDef.Header[$idx] + } + } } $pkg.Save() @@ -242,4 +309,4 @@ function Export-Excel { if($Show) {Invoke-Item $Path} } -} \ No newline at end of file +} diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index e71233d..068e779 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -3,6 +3,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\Export-Excel.ps1 . $PSScriptRoot\New-ConditionalFormattingIconSet.ps1 . $PSScriptRoot\Export-ExcelSheet.ps1 +. $PSScriptRoot\New-ExcelChart.ps1 function Import-Excel { param( @@ -77,17 +78,14 @@ function Add-WorkSheet { [string] $WorkSheetname, [Switch] $NoClobber ) - if($ExcelPackage.Workbook.Worksheets[$WorkSheetname]) { - if($NoClobber) { - $AlreadyExists = $true - Write-Error "Worksheet `"$WorkSheetname`" already exists." - } else { - Write-Debug "Worksheet `"$WorkSheetname`" already exists. Deleting" - $ExcelPackage.Workbook.Worksheets.Delete($WorkSheetname) - } + + $ws = $ExcelPackage.Workbook.Worksheets[$WorkSheetname] + + if(!$ws) { + $ws=$ExcelPackage.Workbook.Worksheets.Add($WorkSheetname) } - $ExcelPackage.Workbook.Worksheets.Add($WorkSheetname) + return $ws } function ConvertFrom-ExcelSheet { diff --git a/README.md b/README.md index 8a7737d..35f376b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,16 @@ Known Issues What's new - +#### 12/17/2015 +These new features open the door for really sophisticated work sheet creation. + +Stay tuned for a [blog post](http://www.dougfinke.com/blog/) and examples. + +***Quick List*** +* StartRow, StartColumn for placing data anywhere in a sheet +* New-ExcelChart - Add charts to a sheet, multiple series for a chart, locate the chart anywhere on the sheet +* AutoNameRange, Use functions and/or calculations in a cell + #### 10/20/2015 Big bug fix for version 3.0 PowerShell folks!