Multiple ways to try the new HeaderName parameter

This commit is contained in:
dfinke
2022-09-09 19:04:08 -04:00
parent b61aef888f
commit bb8297c528
2 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
function ConvertTo-Excel {
param(
$Path,
[System.Collections.IDictionary]$targetData
)
$column = 1
foreach ($key in $targetData.Keys) {
$cityData[$key] | Export-Excel $xlfile -StartColumn ($column++) -HeaderName $key -AutoSize
}
}
$cityData = [Ordered]@{}
$cityData.City = "New York City", "Paris", "Barcelona", "Rome"
$cityData.Country = "United States", "France", "Spain", "Italy"
$cityData.Population = 8600000, 2141000, 5515000, 2873000
$xlfile = "$PSScriptRoot/test.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
ConvertTo-Excel $xlfile $cityData
. $xlfile

View File

@@ -0,0 +1,24 @@
try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return }
## This exports only the numbers
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -Show
## This exports the numbers and in A1 the text "MyNum"
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -HeaderName MyNum -Show
$xlfile = "$PSScriptRoot/testMultipleColumns.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$Regions = 'West', 'North', 'East ', 'East ', 'West ', 'North', 'South', 'North', 'South'
$States = 'Texas', 'Tennessee', 'Florida', 'Maine', 'Virginia', 'Missouri', 'Kansas', 'North Dakota', 'Delaware'
$Units = 927, 466, 520, 828, 465, 436, 214, 789, 712
$Prices = 923.71, 770.67, 458.68, 661.24, 53.58, 235.67, 992.47, 640.72, 508.55
# Export each list (array) as a separate column to the same worksheet and workbook
$Regions | Export-Excel -Path $xlfile -HeaderName Region -StartColumn 1 -AutoSize
$States | Export-Excel -Path $xlfile -HeaderName State -StartColumn 2 -AutoSize
$Units | Export-Excel -Path $xlfile -HeaderName Units -StartColumn 3 -AutoSize
$Prices | Export-Excel -Path $xlfile -HeaderName Prices -StartColumn 4 -AutoSize
# Show the results in Excel
. $xlfile