From 365dd48835b98aeca8d398650c0ca1cb4a3e3717 Mon Sep 17 00:00:00 2001 From: dfinke Date: Wed, 30 Sep 2015 10:26:03 -0400 Subject: [PATCH] Handle data that is _not_ an object #41 --- Export-Excel.ps1 | 66 ++++++++++++++++++++++++++++++++------------ ImportExcel.psd1 | 2 +- PublishToGallery.ps1 | 2 +- README.md | 10 ++++++- 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 1d7c982..d5d8a45 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -86,31 +86,25 @@ function Export-Excel { throw $Error[0].Exception.Message } } + + $firstTimeThru = $true + $isDataTypeValueType=$false + $pattern = "string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort" } Process { - - if(!$Header) { - - $ColumnIndex = 1 - $Header = $TargetData.psobject.properties.name - - foreach ($Name in $Header) { - $ws.Cells[$Row, $ColumnIndex].Value = $name - $ColumnIndex += 1 - } + if($firstTimeThru) { + $firstTimeThru=$false + $isDataTypeValueType = $TargetData.GetType().name -match "string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort" } - $Row += 1 - $ColumnIndex = 1 - - foreach ($Name in $Header) { - + if($isDataTypeValueType) { + $ColumnIndex = 1 + $targetCell = $ws.Cells[$Row, $ColumnIndex] - $cellValue=$TargetData.$Name - $r=$null + $cellValue=$TargetData if([double]::tryparse($cellValue, [ref]$r)) { $targetCell.Value = $r } else { @@ -122,7 +116,43 @@ function Export-Excel { } $ColumnIndex += 1 - } + $Row += 1 + + } else { + 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) { + + $targetCell = $ws.Cells[$Row, $ColumnIndex] + + $cellValue=$TargetData.$Name + + $r=$null + if([double]::tryparse($cellValue, [ref]$r)) { + $targetCell.Value = $r + } else { + $targetCell.Value = $cellValue + } + + switch ($TargetData.$Name) { + {$_ -is [datetime]} {$targetCell.Style.Numberformat.Format = "m/d/yy h:mm"} + } + + $ColumnIndex += 1 + } + } } End { diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index f0f0564..d5d6530 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -4,7 +4,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. -ModuleVersion = '1.8' +ModuleVersion = '1.81' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' diff --git a/PublishToGallery.ps1 b/PublishToGallery.ps1 index 3621e5e..7d9cb4c 100644 --- a/PublishToGallery.ps1 +++ b/PublishToGallery.ps1 @@ -4,7 +4,7 @@ $p = @{ NuGetApiKey = $NuGetApiKey LicenseUri = "https://github.com/dfinke/ImportExcel/blob/master/LICENSE.txt" Tag = "Excel","EPPlus","Export","Import" - ReleaseNote = "Now you can hide worksheets using the -HideSheet paramater" + ReleaseNote = "Can now handle data that is _not_ an object" ProjectUri = "https://github.com/dfinke/ImportExcel" } diff --git a/README.md b/README.md index ce1ada3..1ffdaf5 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ Know Issues What's new - +#### 9/30/2015 + +Export-Excel can now handle data that is **not** an object + + echo a b c 1 $true 2.1 1/1/2015 | Export-Excel c:\temp\test.xlsx -Show +Or + + dir -Name | Export-Excel c:\temp\test.xlsx -Show #### 9/25/2015 @@ -35,7 +43,7 @@ Got a great request from [forensicsguy20012004](https://github.com/forensicsguy2 ##### Example Here, you create four worksheets named `PM`,`Handles`,`Services` and `Files`. -The last line creates the `Files` sheet and then hides the `Handles`,`Services` +The last line creates the `Files` sheet and then hides the `Handles`,`Services` sheets. $p = Get-Process