mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 13:23:29 +00:00
@@ -78,8 +78,7 @@ function Import-Excel {
|
|||||||
$WorkSheetname=1,
|
$WorkSheetname=1,
|
||||||
[int]$HeaderRow=1,
|
[int]$HeaderRow=1,
|
||||||
[string[]]$Header,
|
[string[]]$Header,
|
||||||
[switch]$NoHeader,
|
[switch]$NoHeader
|
||||||
[switch]$DataOnly
|
|
||||||
)
|
)
|
||||||
|
|
||||||
Process {
|
Process {
|
||||||
@@ -99,24 +98,6 @@ function Import-Excel {
|
|||||||
$Columns=$dimension.Columns
|
$Columns=$dimension.Columns
|
||||||
|
|
||||||
if($NoHeader) {
|
if($NoHeader) {
|
||||||
if ($DataOnly) {
|
|
||||||
$CellsWithValues = $worksheet.Cells | where Value
|
|
||||||
|
|
||||||
$Script:i = 0
|
|
||||||
$ColumnReference = $CellsWithValues | Select-Object -ExpandProperty End | Group-Object Column |
|
|
||||||
Select-Object @{L='Column';E={$_.Name}}, @{L='NewColumn';E={$Script:i++; $Script:i}}
|
|
||||||
|
|
||||||
$CellsWithValues | Select-Object -ExpandProperty End | Group-Object Row | ForEach-Object {
|
|
||||||
$newRow = [Ordered]@{}
|
|
||||||
|
|
||||||
foreach ($C in $ColumnReference) {
|
|
||||||
$newRow."P$($C.NewColumn)" = $worksheet.Cells[($_.Name),($C.Column)].Value
|
|
||||||
}
|
|
||||||
|
|
||||||
[PSCustomObject]$newRow
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
foreach ($Row in 0..($Rows-1)) {
|
foreach ($Row in 0..($Rows-1)) {
|
||||||
$newRow = [Ordered]@{}
|
$newRow = [Ordered]@{}
|
||||||
foreach ($Column in 0..($Columns-1)) {
|
foreach ($Column in 0..($Columns-1)) {
|
||||||
@@ -126,7 +107,6 @@ function Import-Excel {
|
|||||||
|
|
||||||
[PSCustomObject]$newRow
|
[PSCustomObject]$newRow
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if(!$Header) {
|
if(!$Header) {
|
||||||
$Header = foreach ($Column in 1..$Columns) {
|
$Header = foreach ($Column in 1..$Columns) {
|
||||||
@@ -136,26 +116,7 @@ function Import-Excel {
|
|||||||
|
|
||||||
if($Rows -eq 1) {
|
if($Rows -eq 1) {
|
||||||
$Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h}
|
$Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if ($DataOnly) {
|
|
||||||
$CellsWithValues = $worksheet.Cells | where Value
|
|
||||||
|
|
||||||
$Script:i = -1
|
|
||||||
$ColumnReference = $CellsWithValues | Select-Object -ExpandProperty End | Group-Object Column |
|
|
||||||
Select-Object @{L='Column';E={$_.Name}}, @{L='NewColumn';E={$Script:i++; $Header[$Script:i]}}
|
|
||||||
|
|
||||||
$CellsWithValues | Select-Object -ExpandProperty End | Group-Object Row | ForEach-Object {
|
|
||||||
$newRow = [Ordered]@{}
|
|
||||||
|
|
||||||
foreach ($C in $ColumnReference) {
|
|
||||||
$newRow."$($C.NewColumn)" = $worksheet.Cells[($_.Name),($C.Column)].Value
|
|
||||||
}
|
|
||||||
|
|
||||||
[PSCustomObject]$newRow
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
foreach ($Row in ($HeaderRow+1)..$Rows) {
|
foreach ($Row in ($HeaderRow+1)..$Rows) {
|
||||||
$h=[Ordered]@{}
|
$h=[Ordered]@{}
|
||||||
foreach ($Column in 0..($Columns-1)) {
|
foreach ($Column in 0..($Columns-1)) {
|
||||||
@@ -168,7 +129,6 @@ function Import-Excel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$stream.Close()
|
$stream.Close()
|
||||||
$stream.Dispose()
|
$stream.Dispose()
|
||||||
|
|||||||
Reference in New Issue
Block a user