Added -NoHeader and returns .Value

This commit is contained in:
dfinke
2015-10-15 11:53:47 -04:00
parent 4b59eb0e75
commit cd46819c79
2 changed files with 33 additions and 14 deletions

View File

@@ -10,7 +10,8 @@ function Import-Excel {
[Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory)] [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory)]
$Path, $Path,
$Sheet=1, $Sheet=1,
[string[]]$Header [string[]]$Header,
[switch]$NoHeader
) )
Process { Process {
@@ -29,6 +30,17 @@ function Import-Excel {
$Rows=$dimension.Rows $Rows=$dimension.Rows
$Columns=$dimension.Columns $Columns=$dimension.Columns
if($NoHeader) {
foreach ($Row in 0..($Rows-1)) {
$newRow = [Ordered]@{}
foreach ($Column in 0..($Columns-1)) {
$propertyName = "P$($Column+1)"
$newRow.$propertyName = $worksheet.Cells[($Row+1),($Column+1)].Value
}
[PSCustomObject]$newRow
}
} else {
if(!$Header) { if(!$Header) {
$Header = foreach ($Column in 1..$Columns) { $Header = foreach ($Column in 1..$Columns) {
$worksheet.Cells[1,$Column].Text $worksheet.Cells[1,$Column].Text
@@ -40,11 +52,12 @@ function Import-Excel {
foreach ($Column in 0..($Columns-1)) { foreach ($Column in 0..($Columns-1)) {
if($Header[$Column].Length -gt 0) { if($Header[$Column].Length -gt 0) {
$Name = $Header[$Column] $Name = $Header[$Column]
$h.$Name = $worksheet.Cells[$Row,($Column+1)].Text $h.$Name = $worksheet.Cells[$Row,($Column+1)].Value
} }
} }
[PSCustomObject]$h [PSCustomObject]$h
} }
}
$stream.Close() $stream.Close()
$stream.Dispose() $stream.Dispose()

View File

@@ -1,4 +1,4 @@
PowerShell Import-Excel goPowerShell Import-Excel
- -
This PowerShell Module wraps the .NET [EPPlus DLL](http://epplus.codeplex.com/) (included). Easily integrate reading and writing Excel spreadsheets into PowerShell, without launching Excel in the background. You can also automate the creation of Pivot Tables and Charts. This PowerShell Module wraps the .NET [EPPlus DLL](http://epplus.codeplex.com/) (included). Easily integrate reading and writing Excel spreadsheets into PowerShell, without launching Excel in the background. You can also automate the creation of Pivot Tables and Charts.
@@ -24,6 +24,12 @@ Know Issues
What's new What's new
- -
#### 10/15/2015
`Import-Excel` has a new parameter `NoHeader`. If data in the sheet does not have headers and you don't want to supply your own, `Import-Excel` will generate the property name.
`Import-Excel` now returns `.Value` rather than `.Text`
#### 10/1/2015 #### 10/1/2015