Further polish to help

This commit is contained in:
jhoneill
2018-09-11 15:34:37 +01:00
parent 3ff59907ff
commit e09c5e5207
7 changed files with 86 additions and 61 deletions

View File

@@ -11,32 +11,33 @@
which will return the rule so that you can modify things which are specific to that type of rule, which will return the rule so that you can modify things which are specific to that type of rule,
for example the values which correspond to each icon in an Icon set. for example the values which correspond to each icon in an Icon set.
.Example .Example
C:\> $excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru >
PS> $excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru
C:\> Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b2:b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003" Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b2:b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003"
C:\> Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled" Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled"
C:\> $excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern $excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern
C:\> $excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true $excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true
C:\> $excel.Save() ; $excel.Dispose() $excel.Save() ; $excel.Dispose()
Here Export-Excel is called with the -passThru parameter so the Excel Package object is stored in $Excel Here Export-Excel is called with the -passThru parameter so the Excel Package object is stored in $Excel
The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show red text if The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show red text if
the columns contain "2003" or "Disabled respectively. A fixed date format is then applied to columns D..G, and the top row is formatted. the columns contain "2003" or "Disabled respectively. A fixed date format is then applied to columns D..G, and the top row is formatted.
Finally the workbook is saved and the Excel object closed. Finally the workbook is saved and the Excel object closed.
.Example .Example
C:\> $r = Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Range "B1:B100" -ThreeIconsSet Flags -Passthru >
C:\> $r.Reverse = $true ; $r.Icon1.Type = "Num"; $r.Icon2.Type = "Num" ; $r.Icon2.value = 100 ; $r.Icon3.type = "Num" ;$r.Icon3.value = 1000 >PS $r = Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Range "B1:B100" -ThreeIconsSet Flags -Passthru
$r.Reverse = $true ; $r.Icon1.Type = "Num"; $r.Icon2.Type = "Num" ; $r.Icon2.value = 100 ; $r.Icon3.type = "Num" ;$r.Icon3.value = 1000
Again Export-Excel has been called with -passthru leaving a package object in $Excel Again Export-Excel has been called with -passthru leaving a package object in $Excel
This time B1:B100 has been conditionally formatted with 3 icons, using the flags icon set. This time B1:B100 has been conditionally formatted with 3 icons, using the flags icon set.
Add-ConditionalFormatting does not provide access to every option in the formatting rule, so passthru has been used and the Add-ConditionalFormatting does not provide access to every option in the formatting rule, so passthru has been used and the
rule is to apply the flags in reverse order, and boundaries for the number which will set the split are set to 100 and 1000 rule is to apply the flags in reverse order, and boundaries for the number which will set the split are set to 100 and 1000
.Example .Example
C:\> Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
This time $sheet holds an ExcelWorkseet object and databars are add to all of column D except for the tip row. This time $sheet holds an ExcelWorkseet object and databars are add to all of column D except for the tip row.
.Example .Example
C:\> Add-ConditionalFormatting -Address $worksheet.cells["FinishPosition"] -RuleType Equal -ConditionValue 1 -ForeGroundColor Purple -Bold -Priority 1 -StopIfTrue Add-ConditionalFormatting -Address $worksheet.cells["FinishPosition"] -RuleType Equal -ConditionValue 1 -ForeGroundColor Purple -Bold -Priority 1 -StopIfTrue
In this example a named range is used to select the cells where the formula should apply. If a cell in the "FinishPosition" range is 1, then the text is turned to puple, boldface. In this example a named range is used to select the cells where the formula should apply. If a cell in the "FinishPosition" range is 1, then the text is turned to puple, boldface.
This rule is move to first in the priority list, and where cells have a value of 1, no other rules will be processed. This rule is move to first in the priority list, and where cells have a value of 1, no other rules will be processed.

View File

@@ -178,7 +178,8 @@
Export all the processes to the Excel file 'Test.xlsx' and open the file immediately. Export all the processes to the Excel file 'Test.xlsx' and open the file immediately.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -190,7 +191,8 @@
Exports all data to the Excel file 'Excel.xslx' and colors the negative values in 'Red' and the positive values in 'Blue'. It will also add a dollar sign '$' in front of the rounded numbers to two decimal characters behind the comma. Exports all data to the Excel file 'Excel.xslx' and colors the negative values in 'Red' and the positive values in 'Blue'. It will also add a dollar sign '$' in front of the rounded numbers to two decimal characters behind the comma.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -215,7 +217,8 @@
Exports all data to the Excel file 'Excel.xslx' and tries to convert all values to numbers where possible except for 'IPAddress' and 'Number1'. These are stored in the sheet 'as is', without being converted to a number. Exports all data to the Excel file 'Excel.xslx' and tries to convert all values to numbers where possible except for 'IPAddress' and 'Number1'. These are stored in the sheet 'as is', without being converted to a number.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -240,7 +243,8 @@
Exports all data to the Excel file 'Excel.xslx' as is, no number conversion will take place. This means that Excel will show the exact same data that you handed over to the 'Export-Excel' function. Exports all data to the Excel file 'Excel.xslx' as is, no number conversion will take place. This means that Excel will show the exact same data that you handed over to the 'Export-Excel' function.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -254,7 +258,8 @@
Exports data that will have a 'Conditional formatting rule' in Excel on these cells that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value is greater then '525'. In case this condition is not met the color will be the default, black text on a white background. Exports data that will have a 'Conditional formatting rule' in Excel on these cells that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value is greater then '525'. In case this condition is not met the color will be the default, black text on a white background.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -269,7 +274,8 @@
Export all services to an Excel sheet where all cells have a 'Conditional formatting rule' in Excel that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value contains the word 'Stop'. If the value contains the word 'Running' it will have a background fill color in 'Cyan' and a text color 'Blue'. In case none of these conditions are met the color will be the default, black text on a white background. Export all services to an Excel sheet where all cells have a 'Conditional formatting rule' in Excel that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value contains the word 'Stop'. If the value contains the word 'Running' it will have a background fill color in 'Cyan' and a text color 'Blue'. In case none of these conditions are met the color will be the default, black text on a white background.
.EXAMPLE .EXAMPLE
$ExcelParams = @{ >
PS> $ExcelParams = @{
Path = $env:TEMP + '\Excel.xlsx' Path = $env:TEMP + '\Excel.xlsx'
Show = $true Show = $true
Verbose = $true Verbose = $true
@@ -312,7 +318,8 @@
Get-Service | Export-Excel 'c:\temp\test.xlsx' -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'} Get-Service | Export-Excel 'c:\temp\test.xlsx' -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'}
.EXAMPLE .EXAMPLE
$pt = [ordered]@{} >
PS> $pt = [ordered]@{}
$pt.pt1=@{ SourceWorkSheet = 'Sheet1'; $pt.pt1=@{ SourceWorkSheet = 'Sheet1';
PivotRows = 'Status' PivotRows = 'Status'
PivotData = @{'Status'='count'} PivotData = @{'Status'='count'}
@@ -335,7 +342,8 @@
.EXAMPLE .EXAMPLE
Remove-Item -Path .\test.xlsx >
PS> Remove-Item -Path .\test.xlsx
$excel = Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -PassThru $excel = Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -PassThru
$excel.Workbook.Worksheets["Sheet1"].Row(1).style.font.bold = $true $excel.Workbook.Worksheets["Sheet1"].Row(1).style.font.bold = $true
$excel.Workbook.Worksheets["Sheet1"].Column(3 ).width = 29 $excel.Workbook.Worksheets["Sheet1"].Column(3 ).width = 29
@@ -348,7 +356,8 @@
It then uses the package object to apply formatting, and then saves the workbook and disposes of the object before loading the document in Excel. It then uses the package object to apply formatting, and then saves the workbook and disposes of the object before loading the document in Excel.
.EXAMPLE .EXAMPLE
Remove-Item -Path .\test.xlsx -ErrorAction Ignore >
PS> Remove-Item -Path .\test.xlsx -ErrorAction Ignore
$excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorksheetName "Processes" -PassThru $excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorksheetName "Processes" -PassThru
$sheet = $excel.Workbook.Worksheets["Processes"] $sheet = $excel.Workbook.Worksheets["Processes"]

View File

@@ -8,30 +8,34 @@
In the former case the header row is copied from the first sheet and, by default, each row of data is labelled with the name of the sheet it came from. In the former case the header row is copied from the first sheet and, by default, each row of data is labelled with the name of the sheet it came from.
In the latter case -NoHeader is specified, and each copied block can have the sheet it came from placed above it as a title. In the latter case -NoHeader is specified, and each copied block can have the sheet it came from placed above it as a title.
.EXAMPLE .EXAMPLE
foreach ($computerName in @('Server1', 'Server2', 'Server3', 'Server4')) {
Get-Service -ComputerName $computerName | Select-Object -Property Status, Name, DisplayName, StartType |
Export-Excel -Path .\test.xlsx -WorkSheetname $computerName -AutoSize
}
$ptDef =New-PivotTableDefinition -PivotTableName "Pivot1" -SourceWorkSheet "Combined" -PivotRows "Status" -PivotFilter "MachineName" -PivotData @{Status='Count'} -IncludePivotChart -ChartType BarClustered3D
Join-Worksheet -Path .\test.xlsx -WorkSheetName combined -FromLabel "MachineName" -HideSource -AutoSize -FreezeTopRow -BoldTopRow -PivotTableDefinition $pt -Show
The foreach command gets the services running on four servers and exports each to its own page in Test.xlsx.
$PtDef= creates a defintion for a single Pivot table.
The Join-Worksheet command uses the same file and merges the results onto a sheet named "Combined". It sets a column header of "Machinename",
this column will contain the name of the sheet the data was copied from; after copying the data to the sheet "combined", the other sheets will be hidden.
Join-Worksheet finishes by calling export-Excel to AutoSize cells, freeze the top row and make it bold and add the Pivot table.
.EXAMPLE .EXAMPLE
Get-WmiObject -Class win32_logicaldisk | select -Property DeviceId,VolumeName, Size,Freespace | >
Export-Excel -Path "$env:computerName.xlsx" -WorkSheetname Volumes -NumberFormat "0,000" PS> foreach ($computerName in @('Server1', 'Server2', 'Server3', 'Server4')) {
Get-NetAdapter | Select-Object Name,InterfaceDescription,MacAddress,LinkSpeed | Get-Service -ComputerName $computerName | Select-Object -Property Status, Name, DisplayName, StartType |
Export-Excel -Path "$env:COMPUTERNAME.xlsx" -WorkSheetname NetAdapter Export-Excel -Path .\test.xlsx -WorkSheetname $computerName -AutoSize
Join-Worksheet -Path "$env:COMPUTERNAME.xlsx" -WorkSheetName Summary -Title "Summary" -TitleBold -TitleSize 22 -NoHeader -LabelBlocks -AutoSize -HideSource -show }
$ptDef =New-PivotTableDefinition -PivotTableName "Pivot1" -SourceWorkSheet "Combined" -PivotRows "Status" -PivotFilter "MachineName" -PivotData @{Status='Count'} -IncludePivotChart -ChartType BarClustered3D
Join-Worksheet -Path .\test.xlsx -WorkSheetName combined -FromLabel "MachineName" -HideSource -AutoSize -FreezeTopRow -BoldTopRow -PivotTableDefinition $pt -Show
The first two command get logical disk and network card information; each type is exported to its own sheet in a workbook. The foreach command gets the services running on four servers and exports each to its own page in Test.xlsx.
The Join-worksheet command copies both onto a page named "Summary". Because the data is disimilar -NoHeader is specified, ensuring the whole of each page is copied. $PtDef= creates a defintion for a single Pivot table.
Specifying -LabelBlocks causes each sheet's name to become a title on the summary page above the copied data. The Join-Worksheet command uses the same file and merges the results onto a sheet named "Combined". It sets a column header of "Machinename",
The source data is hidden, a title is added in 22 point boldface and the columns are sized to fit the data. this column will contain the name of the sheet the data was copied from; after copying the data to the sheet "combined", the other sheets will be hidden.
Join-Worksheet finishes by calling export-Excel to AutoSize cells, freeze the top row and make it bold and add the Pivot table.
.EXAMPLE
>
PS> Get-WmiObject -Class win32_logicaldisk | select -Property DeviceId,VolumeName, Size,Freespace |
Export-Excel -Path "$env:computerName.xlsx" -WorkSheetname Volumes -NumberFormat "0,000"
Get-NetAdapter | Select-Object Name,InterfaceDescription,MacAddress,LinkSpeed |
Export-Excel -Path "$env:COMPUTERNAME.xlsx" -WorkSheetname NetAdapter
Join-Worksheet -Path "$env:COMPUTERNAME.xlsx" -WorkSheetName Summary -Title "Summary" -TitleBold -TitleSize 22 -NoHeader -LabelBlocks -AutoSize -HideSource -show
The first two commands get logical disk and network card information; each type is exported to its own sheet in a workbook.
The Join-worksheet command copies both onto a page named "Summary". Because the data is disimilar -NoHeader is specified, ensuring the whole of each page is copied.
Specifying -LabelBlocks causes each sheet's name to become a title on the summary page above the copied data.
The source data is hidden, a title is added in 22 point boldface and the columns are sized to fit the data.
#> #>
[CmdletBinding(DefaultParameterSetName = 'Default')] [CmdletBinding(DefaultParameterSetName = 'Default')]
param ( param (

View File

@@ -82,7 +82,8 @@ function New-ExcelChartDefinition {
.PARAMETER Header .PARAMETER Header
No longer used. This may be removed in future versions No longer used. This may be removed in future versions
.Example .Example
$cDef = New-ExcelChartDefinition -ChartType line -XRange "X" -YRange "Sinx" -Title "Graph of Sine X" -TitleBold -TitleSize 14 ` >
PS> $cDef = New-ExcelChartDefinition -ChartType line -XRange "X" -YRange "Sinx" -Title "Graph of Sine X" -TitleBold -TitleSize 14 `
-Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000" ` -Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000" `
-YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 ` -YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 `
-SeriesHeader "Sin(x)" -LegendSize 8 -legendBold -LegendPostion Bottom -SeriesHeader "Sin(x)" -LegendSize 8 -legendBold -LegendPostion Bottom
@@ -265,7 +266,8 @@ function Add-ExcelChart {
.PARAMETER PassThru .PARAMETER PassThru
Add-Excel chart doesn't normally return anything, but if -PassThru is specified it returns the newly created chart to allow it to be fine tuned Add-Excel chart doesn't normally return anything, but if -PassThru is specified it returns the newly created chart to allow it to be fine tuned
.EXAMPLE .EXAMPLE
$Excel = ConvertFrom-Csv @" >
PS> $Excel = ConvertFrom-Csv @"
Product, City, Sales Product, City, Sales
Apple, London , 300 Apple, London , 300
Orange, London , 400 Orange, London , 400
@@ -282,15 +284,17 @@ function Add-ExcelChart {
combine the name of the product and the name of the City to create the table. combine the name of the product and the name of the City to create the table.
The width of the chart is set explictly, the default legend is used and there is no Chart title. The width of the chart is set explictly, the default legend is used and there is no Chart title.
.EXAMPLE .EXAMPLE
$Excel = Invoke-Sum (Get-Process) Company Handles, PM, VirtualMemorySize | Export-Excel $path -AutoSize -ExcelChartDefinition $c -AutoNameRange -PassThru >
PS> $Excel = Invoke-Sum (Get-Process) Company Handles, PM, VirtualMemorySize | Export-Excel $path -AutoSize -ExcelChartDefinition $c -AutoNameRange -PassThru
Add-ExcelChart -Worksheet $Excel.Workbook.Worksheets[1] -Title "VM use" -ChartType PieExploded3D -XRange "Name" -YRange "VirtualMemorySize" -NoLegend -ShowCategory Add-ExcelChart -Worksheet $Excel.Workbook.Worksheets[1] -Title "VM use" -ChartType PieExploded3D -XRange "Name" -YRange "VirtualMemorySize" -NoLegend -ShowCategory
Close-ExcelPackage $Excel -Show Close-ExcelPackage $Excel -Show
The first line exports information and creates named ranges for each column. The first line exports information and creates named ranges for each column.
The Second line uses the ranges to specify a chart - the labels come from the range "Name" and the data from the range "VirtualMemorySize" The Second line uses the ranges to specify a chart - the labels come from the range "Name" and the data from the range "VirtualMemorySize"
The chart is specified as a 3D exploded PIE chart, with a title of "VM Use" and instead of a legend the the pie slices are identified with a label. The chart is specified as a 3D exploded PIE chart, with a title of "VM Use" and instead of a legend the the pie slices are identified with a label.
.EXAMPLE .EXAMPLE
$Excel = Invoke-Sum (Get-Process) Company Handles, PM, VirtualMemorySize | Export-Excel test.xlsx -TableName Processes -PassThru >
PS> $Excel = Invoke-Sum (Get-Process) Company Handles, PM, VirtualMemorySize | Export-Excel test.xlsx -TableName Processes -PassThru
Add-ExcelChart -Worksheet $Excel.Workbook.Worksheets[1] -Title Stats -ChartType LineMarkersStacked -XRange "Processes[Name]" -YRange "Processes[PM]", "Processes[VirtualMemorySize]" -SeriesHeader 'PM', 'VMSize' Add-ExcelChart -Worksheet $Excel.Workbook.Worksheets[1] -Title Stats -ChartType LineMarkersStacked -XRange "Processes[Name]" -YRange "Processes[PM]", "Processes[VirtualMemorySize]" -SeriesHeader 'PM', 'VMSize'
Close-ExcelPackage $Excel -Show Close-ExcelPackage $Excel -Show
@@ -298,7 +302,8 @@ function Add-ExcelChart {
The second line creates a chart on the first page of the work sheet, using the notation "TableName[ColumnnName]" to refer to the data, The second line creates a chart on the first page of the work sheet, using the notation "TableName[ColumnnName]" to refer to the data,
the labels come Name column in the table, and the data series from its PM and VirtualMemorySize columns. The display names for these in the header are set to PM and VMSize the labels come Name column in the table, and the data series from its PM and VirtualMemorySize columns. The display names for these in the header are set to PM and VMSize
.EXAMPLE .EXAMPLE
$excel = 0..360 | ForEach-Object {[pscustomobject][ordered]@{x = $_; Sinx = "=Sin(Radians(x)) "}} | Export-Excel -AutoNameRange -Path Text.xlsx -WorkSheetname SinX -PassThru >
PS> $excel = 0..360 | ForEach-Object {[pscustomobject][ordered]@{x = $_; Sinx = "=Sin(Radians(x)) "}} | Export-Excel -AutoNameRange -Path Text.xlsx -WorkSheetname SinX -PassThru
Add-ExcelChart -Worksheet $excel.Workbook.Worksheets["Sinx"] -ChartType line -XRange "X" -YRange "Sinx" -Title "Graph of Sine X" -TitleBold -TitleSize 14 ` Add-ExcelChart -Worksheet $excel.Workbook.Worksheets["Sinx"] -ChartType line -XRange "X" -YRange "Sinx" -Title "Graph of Sine X" -TitleBold -TitleSize 14 `
-Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000" ` -Column 2 -ColumnOffSetPixels 35 -Width 800 -XAxisTitleText "Degrees" -XAxisTitleBold -XAxisTitleSize 12 -XMajorUnit 30 -XMinorUnit 10 -XMinValue 0 -XMaxValue 361 -XAxisNumberformat "000" `
-YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 ` -YMinValue -1.25 -YMaxValue 1.25 -YMajorUnit 0.25 -YAxisNumberformat "0.00" -YAxisTitleText "Sine" -YAxisTitleBold -YAxisTitleSize 12 `

View File

@@ -8,13 +8,15 @@
It takes a KillExcel switch to make sure Excel is not holding the file open; a password parameter for existing protected files, It takes a KillExcel switch to make sure Excel is not holding the file open; a password parameter for existing protected files,
and a create switch to set-up a new file if no file already exists. and a create switch to set-up a new file if no file already exists.
.Example .Example
$excel = Open-ExcelPackage -Path "$env:TEMP\test99.xlsx" -Create >
PS> $excel = Open-ExcelPackage -Path "$env:TEMP\test99.xlsx" -Create
$ws = Add-WorkSheet -ExcelPackage $excel $ws = Add-WorkSheet -ExcelPackage $excel
This will create a new file in the temp folder if it doesn't already exist. It then adds a worksheet - This will create a new file in the temp folder if it doesn't already exist. It then adds a worksheet -
because no name is specified it will use the default name of "Sheet1" because no name is specified it will use the default name of "Sheet1"
.Example .Example
$excel = Open-ExcelPackage -path "$xlPath" -Password $password >
PS> $excel = Open-ExcelPackage -path "$xlPath" -Password $password
$sheet1 = $excel.Workbook.Worksheets["sheet1"] $sheet1 = $excel.Workbook.Worksheets["sheet1"]
Set-ExcelRange -Range $sheet1.Cells["E1:S1048576"], $sheet1.Cells["V1:V1048576"] -NFormat ([cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern) Set-ExcelRange -Range $sheet1.Cells["E1:S1048576"], $sheet1.Cells["V1:V1048576"] -NFormat ([cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern)
Close-ExcelPackage $excel -Show Close-ExcelPackage $excel -Show

View File

@@ -5,15 +5,17 @@
.Description .Description
If the pivot table already exists, the source data will be updated. If the pivot table already exists, the source data will be updated.
.Example .Example
$excel = Get-Service | Export-Excel -Path test.xlsx -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName ServiceTable -Title "Services on $Env:COMPUTERNAME" >
PS> $excel = Get-Service | Export-Excel -Path test.xlsx -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName ServiceTable -Title "Services on $Env:COMPUTERNAME"
Add-PivotTable -ExcelPackage $excel -PivotTableName ServiceSummary -SourceRange $excel.Workbook.Worksheets[1].Tables[0].Address -PivotRows Status -PivotData Name -NoTotalsInPivot -Activate Add-PivotTable -ExcelPackage $excel -PivotTableName ServiceSummary -SourceRange $excel.Workbook.Worksheets[1].Tables[0].Address -PivotRows Status -PivotData Name -NoTotalsInPivot -Activate
Close-ExcelPackage $excel -Show Close-ExcelPackage $excel -Show
This exports data to new workbook and creates a table with the data in. This exports data to new workbook and creates a table with the data in.
The Pivot table is added on its own page, the table created in the first command is used as the source for the PivotTable; which counts the service names in for each Status. At the end the Pivot page is made active. The Pivot table is added on its own page, the table created in the first command is used as the source for the PivotTable; which counts the service names in for each Status. At the end the Pivot page is made active.
.Example .Example
$chartdef = New-ExcelChartDefinition -Title "Gross and net by city and product" -ChartType ColumnClustered ` >
-Column 11 -Width 500 -Height 360 -YMajorUnit 500 -YMinorUnit 100 -YAxisNumberformat "$#,##0" -LegendPostion Bottom PS> $chartdef = New-ExcelChartDefinition -Title "Gross and net by city and product" -ChartType ColumnClustered `
-Column 11 -Width 500 -Height 360 -YMajorUnit 500 -YMinorUnit 100 -YAxisNumberformat "$#,##0" -LegendPostion Bottom
$excel = ConvertFrom-Csv @" $excel = ConvertFrom-Csv @"
Product, City, Gross, Net Product, City, Gross, Net
@@ -30,6 +32,7 @@
-PivotNumberFormat "$#,##0.00" -PivotTotals Both -PivotTableSyle Medium12 -PivotChartDefinition $chartdef -PivotNumberFormat "$#,##0.00" -PivotTotals Both -PivotTableSyle Medium12 -PivotChartDefinition $chartdef
Close-ExcelPackage -show $excel Close-ExcelPackage -show $excel
This script starts by defining a chart. Then it exports some data to an XLSX file and keeps the file open. This script starts by defining a chart. Then it exports some data to an XLSX file and keeps the file open.
The next step is to add the pivot table, normally this would be on its own sheeet in the workbook, The next step is to add the pivot table, normally this would be on its own sheeet in the workbook,
but here -Address is specified to place it beside the data. The Add-Pivot table is given the chart definition and told to create a tale but here -Address is specified to place it beside the data. The Add-Pivot table is given the chart definition and told to create a tale
@@ -226,15 +229,16 @@
function New-PivotTableDefinition { function New-PivotTableDefinition {
<# <#
.Synopsis .Synopsis
Creates Pivot table definitons for Export-Excel Creates Pivot table definitons for Export-Excel
.Description .Description
Export-Excel allows a single Pivot table to be defined using the parameters -IncludePivotTable, -PivotColumns -PivotRows, Export-Excel allows a single Pivot table to be defined using the parameters -IncludePivotTable, -PivotColumns -PivotRows,
=PivotData, -PivotFilter, -PivotTotals, -PivotDataToColumn, -IncludePivotChart and -ChartType. =PivotData, -PivotFilter, -PivotTotals, -PivotDataToColumn, -IncludePivotChart and -ChartType.
Its -PivotTableDefintion paramater allows multiple pivot tables to be defined, with additional parameters. Its -PivotTableDefintion paramater allows multiple pivot tables to be defined, with additional parameters.
New-PivotTableDefinition is a convenient way to build these definitions. New-PivotTableDefinition is a convenient way to build these definitions.
.Example .Example
$pt = New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet "Sheet1" -PivotRows "Status" -PivotData @{Status='Count' } -PivotFilter 'StartType' -IncludePivotChart -ChartType BarClustered3D >
PS> $pt = New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet "Sheet1" -PivotRows "Status" -PivotData @{Status='Count' } -PivotFilter 'StartType' -IncludePivotChart -ChartType BarClustered3D
$Pt += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet "Sheet2" -PivotRows "Company" -PivotData @{Company='Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -ChartTitle "Breakdown of processes by company" $Pt += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet "Sheet2" -PivotRows "Company" -PivotData @{Company='Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -ChartTitle "Breakdown of processes by company"
Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -AutoSize Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -AutoSize
Get-Process | Select-Object -Property Name,Company,Handles,CPU,VM | Export-Excel -Path .\test.xlsx -AutoSize -WorksheetName 'sheet2' Get-Process | Select-Object -Property Name,Company,Handles,CPU,VM | Export-Excel -Path .\test.xlsx -AutoSize -WorksheetName 'sheet2'

View File

@@ -8,19 +8,19 @@
A column heading can be specified and the new column can be made a named range. A column heading can be specified and the new column can be made a named range.
The column can be formatted in the same operation. The column can be formatted in the same operation.
.EXAMPLE .EXAMPLE
C:\> Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency' Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
$ws contains a worksheet object - and column E is set to use the local currecy format. $ws contains a worksheet object - and column E is set to use the local currecy format.
Intelisense will complete predefined number formats. You can see how currency is interpreted on the local computer with the command Intelisense will complete predefined number formats. You can see how currency is interpreted on the local computer with the command
Expand-NumberFormat currency Expand-NumberFormat currency
.EXAMPLE .EXAMPLE
C:\> Set-ExcelColumn -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange Set-ExcelColumn -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E) Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E)
Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc
the data cells should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically the data cells should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically
.EXAMPLE .EXAMPLE
C:\> Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize
In this example, the worksheet in $ws has partial links to wikipedia pages in column B. In this example, the worksheet in $ws has partial links to wikipedia pages in column B.
The Value parameter is is a script block and it outputs a string which begins https... and ends with the value of cell at column B in the current row. The Value parameter is is a script block and it outputs a string which begins https... and ends with the value of cell at column B in the current row.