mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Append, and formatting
Changed Export-Excel.ps1 #1 @ Line 197 Made new parameter sets . Default, and table already existed and use path. Added DefaultPackage and TablePackage A New parameter "Package" allows an ExcelPackage object returned by -passThru to be passed in ~Line 400 code to use package or path depending on path passed. (also added Open-ExcelPackage to get the object without exporting and Close-ExcelPackage to close it nicely - these are in their own file) #2. @ Line 256 added new parameter excludeProperty to remove unwanted properties without needing to go through select-object ~Line 459 added logic to exclude the properties specified in the new parameter #3 . @ Line 262 Added new parameter Append ~Line 420 code to read the existing headers and move the insertion point below the current data (normal behaviour is to check if headers exist when adding data in the process block, which makes this change wonderfully easy) ~Line 510 changed basis for identifying named ranges and changed scope for rangeName so it can be used on other sheets #4. ~Line 550. Remove any existing Pivot table before trying to [re]create it. Added formatting.ps1 which applies conditional and normal formats - requires an ExcelPackage to be open. Added Open-ExcelPackage.ps1 (which contains a close function as well to get the the object and save it ) open allows the sheet to be loaded into a package object without needing to export . Updated .psm1 to add the formating and open/close ps1 files.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Function Export-Excel {
|
||||
Function Export-Excel {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Export data to an Excel worksheet.
|
||||
@@ -196,8 +196,13 @@ Function Export-Excel {
|
||||
|
||||
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
||||
Param(
|
||||
$Path,
|
||||
[Parameter(ValueFromPipeline = $true)]
|
||||
[Parameter(Mandatory=$true,ParameterSetName="Default",Position=0)]
|
||||
[Parameter(Mandatory=$true,ParameterSetName="Table" ,Position=0)]
|
||||
[String]$Path,
|
||||
[Parameter(Mandatory=$true,ParameterSetName="PackageDefault")]
|
||||
[Parameter(Mandatory=$true,ParameterSetName="PackageTable")]
|
||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||
[Parameter(ValueFromPipeline=$true)]
|
||||
$TargetData,
|
||||
[String]$WorkSheetname = 'Sheet1',
|
||||
[String]$Title,
|
||||
@@ -225,6 +230,7 @@ Function Export-Excel {
|
||||
[Switch]$FreezeTopRowFirstColumn,
|
||||
[Int[]]$FreezePane,
|
||||
[Parameter(ParameterSetName = 'Default')]
|
||||
[Parameter(ParameterSetName = 'PackageDefault')]
|
||||
[Switch]$AutoFilter,
|
||||
[Switch]$BoldTopRow,
|
||||
[Switch]$NoHeader,
|
||||
@@ -244,8 +250,10 @@ Function Export-Excel {
|
||||
}
|
||||
})]
|
||||
[Parameter(ParameterSetName = 'Table')]
|
||||
[Parameter(ParameterSetName = 'PackageTable')]
|
||||
[String]$TableName,
|
||||
[Parameter(ParameterSetName = 'Table')]
|
||||
[Parameter(ParameterSetName = 'PackageTable')]
|
||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6',
|
||||
[Object[]]$ExcelChartDefinition,
|
||||
[String[]]$HideSheet,
|
||||
@@ -255,11 +263,13 @@ Function Export-Excel {
|
||||
[Int]$StartColumn = 1,
|
||||
[Switch]$PassThru,
|
||||
[String]$Numberformat = 'General',
|
||||
[string[]]$ExcludeProperty,
|
||||
[String[]]$NoNumberConversion,
|
||||
[Object[]]$ConditionalFormat,
|
||||
[Object[]]$ConditionalText,
|
||||
[ScriptBlock]$CellStyleSB,
|
||||
[Switch]$Now
|
||||
[Switch]$Now,
|
||||
[switch]$Append
|
||||
)
|
||||
|
||||
Begin {
|
||||
@@ -354,7 +364,7 @@ Function Export-Excel {
|
||||
if ($TitleBackgroundColor -AND ($TitleFillPattern -ne 'None')) {
|
||||
$ws.Cells[$Row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
|
||||
}
|
||||
else {
|
||||
elseif ($TitleBackgroundColor) {
|
||||
Write-Warning "Title Background Color ignored. You must set the TitleFillPattern parameter to a value other than 'None'. Try 'Solid'."
|
||||
}
|
||||
}
|
||||
@@ -403,35 +413,52 @@ Function Export-Excel {
|
||||
}
|
||||
}
|
||||
|
||||
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
||||
if ($ExcelPackage) {
|
||||
$pkg = $ExcelPackage
|
||||
$Path = $pkg.File
|
||||
}
|
||||
Else {
|
||||
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
||||
|
||||
if (Test-Path $Path) {
|
||||
Write-Debug "Path '$Path' already exists"
|
||||
}
|
||||
|
||||
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
||||
$ws = $pkg | Add-WorkSheet -WorkSheetname $WorkSheetname -NoClobber:$NoClobber
|
||||
if (Test-Path $Path) {
|
||||
Write-Debug "Path '$Path' already exists"
|
||||
}
|
||||
|
||||
$pkg = New-Object OfficeOpenXml.ExcelPackage $Path
|
||||
}
|
||||
|
||||
[OfficeOpenXml.ExcelWorksheet]$ws = $pkg | Add-WorkSheet -WorkSheetname $WorkSheetname -NoClobber:$NoClobber
|
||||
|
||||
foreach ($format in $ConditionalFormat ) {
|
||||
$target = "Add$($format.Formatter)"
|
||||
$rule = ($ws.ConditionalFormatting).PSObject.Methods[$target].Invoke($format.Range, $format.IconType)
|
||||
$rule.Reverse = $format.Reverse
|
||||
}
|
||||
|
||||
$Row = $StartRow
|
||||
|
||||
if ($Title) {
|
||||
Add-Title
|
||||
|
||||
$Row += 1
|
||||
if ($append) {
|
||||
$headerRange = $ws.Dimension.Address -replace "\d+$","1"
|
||||
#if there is a title or anything else above the header row, specifying StartRow will skip it.
|
||||
if ($StartRow -ne 1) {$headerRange = $headerRange -replace "1","$StartRow"}
|
||||
$script:Header = $ws.Cells[$headerrange].Value
|
||||
$row = $ws.Dimension.Rows
|
||||
Write-Debug -Message ("Appending: headers are " + ($script:Header -join ", ") + "Start row $row")
|
||||
}
|
||||
|
||||
elseif($Title) { #Can only add a title if not appending
|
||||
$Row = $StartRow
|
||||
Add-Title
|
||||
$Row ++
|
||||
}
|
||||
else {
|
||||
$Row = $StartRow
|
||||
|
||||
}
|
||||
$ColumnIndex = $StartColumn
|
||||
$firstTimeThru = $true
|
||||
$isDataTypeValueType = $false
|
||||
$pattern = 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort'
|
||||
}
|
||||
Catch {
|
||||
if ($AlreadyExists) {
|
||||
if ($AlreadyExists) { #Is this set anywhere ?
|
||||
throw "Failed exporting worksheet '$WorkSheetname' to '$Path': The worksheet '$WorkSheetname' already exists."
|
||||
}
|
||||
else {
|
||||
@@ -460,7 +487,7 @@ Function Export-Excel {
|
||||
#region Add headers
|
||||
if (-not $script:Header) {
|
||||
$ColumnIndex = $StartColumn
|
||||
$script:Header = $TargetData.PSObject.Properties.Name
|
||||
$script:Header = $TargetData.PSObject.Properties.Name | Where-Object {$_ -notin $ExcludeProperty}
|
||||
|
||||
if ($NoHeader) {
|
||||
# Don't push the headers to the spread sheet
|
||||
@@ -536,7 +563,9 @@ Function Export-Excel {
|
||||
$cer = $ws.Dimension.End.Row
|
||||
$cec = $script:Header.Count
|
||||
|
||||
$targetRange = $ws.Cells[$csr, $csc, $cer, $cec]
|
||||
$targetRange = $ws.Cells[$csr, $csc, $cer, $cec]
|
||||
#if we're appending data the table may already exist: but excel doesn't like the result if I put
|
||||
# if ($ws.Tables[$TableName]) {$ws.Tables.Delete($TableName) }
|
||||
$tbl = $ws.Tables.Add($targetRange, $TableName)
|
||||
$tbl.TableStyle = $TableStyle
|
||||
}
|
||||
@@ -545,6 +574,8 @@ Function Export-Excel {
|
||||
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
|
||||
$targetName = $item.Key
|
||||
$pivotTableName = $targetName #+ 'PivotTable'
|
||||
#Make sure the Pivot table sheet doesn't already exist
|
||||
try { $pkg.Workbook.Worksheets.Delete( $pivotTableName) } catch {}
|
||||
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
||||
$pivotTableDataName = $targetName + 'PivotTableData'
|
||||
|
||||
@@ -611,6 +642,8 @@ Function Export-Excel {
|
||||
|
||||
if ($IncludePivotTable) {
|
||||
$pivotTableName = $WorkSheetname + 'PivotTable'
|
||||
#Make sure the Pivot table sheet doesn't already exist
|
||||
try { $pkg.Workbook.Worksheets.Delete( $pivotTableName) } catch {}
|
||||
$wsPivot = $pkg | Add-WorkSheet -WorkSheetname $pivotTableName -NoClobber:$NoClobber
|
||||
|
||||
$wsPivot.View.TabSelected = $true
|
||||
|
||||
Reference in New Issue
Block a user