Handle data that is _not_ an object #41

This commit is contained in:
dfinke
2015-09-30 10:26:03 -04:00
parent 9f718635b2
commit 365dd48835
4 changed files with 59 additions and 21 deletions

View File

@@ -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 {

View File

@@ -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'

View File

@@ -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"
}

View File

@@ -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