Rewrote 'Import-Excel':

- Added parameter sets for proper parameter validation and to make sure '-NoHeader' and '-HeaderRow' aren't used together
- Added try/catch clause, CmdLetBinding and verbose messages
- Renamed 'HeaderRow' to 'TopRow' to avoid confusion with other parameters
- Renamed '-Header' to '-HeaderName'
- Added test for duplicate property names
- Added test for empty worksheet
- Added test for no data after TopRow
- Fixed incorrect import when there's no value in the first column
- Fixed values being imported under the wrong property name in case one
- Fixed incorrect import in case column A is empty and B and C not ( '$Worksheet.Dimension.Columns' is unreliable because it will say 2 columns are in use while it should say 3).
(Ex. Add data in cell B2 and C2, use the '-NoHeader' switch, notice P1 and P2 are incorrectly blanc.)
This commit is contained in:
DarkLite1
2017-08-21 15:34:30 +02:00
parent d8d624ba9c
commit 662d5913ae
2 changed files with 345 additions and 119 deletions

View File

@@ -4,7 +4,7 @@
RootModule = 'ImportExcel.psm1'
# Version number of this module.
ModuleVersion = '3.0.2'
ModuleVersion = '3.0.3'
# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'

View File

@@ -42,147 +42,373 @@ else {
Write-Warning 'PowerShell Excel is ready, except for that functionality'
}
Function Import-Excel {
<#
<#
.SYNOPSIS
Read the content of an Excel sheet.
Create custom objects from the rows in an Excel worksheet.
.DESCRIPTION
The Import-Excel cmdlet reads the content of an Excel worksheet and creates one object for each row. This is done without using Microsoft Excel in the background but by using the .NET EPPLus.dll. You can also automate the creation of Pivot Tables and Charts.
The Import-Excel cmdlet creates custom objects from the rows in an Excel worksheet. Each row represents one object. All of this is possible without installing Microsoft Excel and by using the .NET library <20>EPPLus.dll<EFBFBD>.
By default, the property names of the objects are retrieved from the column headers. Because an object cannot have a blanc property name, only columns with column headers will be imported.
If the default behavior is not desired and you want to import the complete worksheet <20>as is<69>, the parameter <20>-NoHeader<65> can be used. In case you want to provide your own property names, you can use the parameter <20>-HeaderName<6D>.
.PARAMETER Path
Specifies the path to the Excel file.
.PARAMETER WorkSheetname
Specifies the name of the worksheet in the Excel workbook.
.PARAMETER HeaderRow
Specifies custom header names for columns.
.PARAMETER Header
Specifies the title used in the worksheet. The title is placed on the first line of the worksheet.
.PARAMETER NoHeader
When used we generate our own headers (P1, P2, P3, ..) instead of the ones defined in the first row of the Excel worksheet.
.PARAMETER WorksheetName
Specifies the name of the worksheet in the Excel workbook to import.
.PARAMETER DataOnly
When used we will only generate objects for rows that contain text values, not for empty rows or columns.
.EXAMPLE
Import-Excel -WorkSheetname 'Statistics' -Path 'E:\Finance\Company results.xlsx'
Imports all the information found in the worksheet 'Statistics' of the Excel file 'Company results.xlsx'
Import only rows and columns that contain data, empty rows and empty columns are not imported.
.PARAMETER HeaderName
Specifies custom property names to use, instead of the values defined in the column headers of the TopRow.
In case you provide less header names than there is data in the worksheet, then only the data with a corresponding header name will be imported and the data without header name will be disregarded.
In case you provide more header names than there is data in the worksheet, then all data will be imported and all objects will have all the property names you defined in the header names. As such, the last properties will be blanc as there is no data for them.
.PARAMETER NoHeader
Automatically generate property names (P1, P2, P3, ..) instead of the ones defined in the column headers of the TopRow.
This switch is best used when you want to import the complete worksheet <20>as is<69> and are not concerned with the property names.
.PARAMETER TopRow
The row from where we start to import data, all rows above the TopRow are disregarded. By default this is the first row.
When the parameters <20>-NoHeader<65> and <20>-HeaderName<6D> are not provided, this row will contain the column headers that will be used as property names. When one of both parameters are provided, the property names are automatically created and this row will be treated as a regular row containing data.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the column names defined in the first row. In case a column doesn<73>t have a column header (usually in row 1 when <20>-TopRow<6F> is not used), then the unnamed columns will be skipped and the data in those columns will not be imported.
----------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
----------------------------------------------
| A B C |
|1 First Name Address |
|2 Chuck Norris California |
|3 Jean-Claude Vandamme Brussels |
----------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Actors
First Name: Chuck
Address : California
First Name: Jean-Claude
Address : Brussels
Notice that column 'B' is not imported because there's no value in cell 'B1' that can be used as property name for the objects.
.EXAMPLE
Import the complete Excel worksheet <20>as is<69> by using the <20>-NoHeader<65> switch. One object is created for each row. The property names of the objects will be automatically generated (P1, P2, P3, ..).
----------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
----------------------------------------------
| A B C |
|1 First Name Address |
|2 Chuck Norris California |
|3 Jean-Claude Vandamme Brussels |
----------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Actors -NoHeader
P1: First Name
P2:
P3: Address
P1: Chuck
P2: Norris
P3: California
P1: Jean-Claude
P2: Vandamme
P3: Brussels
Notice that the column header (row 1) is imported as an object too.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the names defined in the parameter <20>-HeaderName<6D>. The properties are named starting from the most left column (A) to the right. In case no value is present in one of the columns, that property will have an empty value.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Movies |
----------------------------------------------------------
| A B C D |
|1 The Bodyguard 1992 9 |
|2 The Matrix 1999 8 |
|3 |
|4 Skyfall 2012 9 |
----------------------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Movies -HeaderName 'Movie name', 'Year', 'Rating', 'Genre'
Movie name: The Bodyguard
Year : 1992
Rating : 9
Genre :
Movie name: The Matrix
Year : 1999
Rating : 8
Genre :
Movie name:
Year :
Rating :
Genre :
Movie name: Skyfall
Year : 2012
Rating : 9
Genre :
Notice that empty rows are imported and that data for the property 'Genre' is not present in the worksheet. As such, the 'Genre' property will be blanc for all objects.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects are automatically generated by using the switch <20>-NoHeader<65> (P1, P@, P#, ..). The switch <20>-DataOnly<6C> will speed up the import because empty rows and empty columns are not imported.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Movies |
----------------------------------------------------------
| A B C D |
|1 The Bodyguard 1992 9 |
|2 The Matrix 1999 8 |
|3 |
|4 Skyfall 2012 9 |
----------------------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Movies <20>NoHeader -DataOnly
P1: The Bodyguard
P2: 1992
P3: 9
P1: The Matrix
P2: 1999
P3: 8
P1: Skyfall
P2: 2012
P3: 9
Notice that empty rows and empty columns are not imported.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names are provided with the <20>-HeaderName<6D> parameter. The import will start from row 2 and empty columns and rows are not imported.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
----------------------------------------------------------
| A B C D |
|1 Chuck Norris California |
|2 |
|3 Jean-Claude Vandamme Brussels |
----------------------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Actors -DataOnly -HeaderName 'FirstName', 'SecondName', 'City' <20>TopRow 2
FirstName : Jean-Claude
SecondName: Vandamme
City : Brussels
Notice that only 1 object is imported with only 3 properties. Column B and row 2 are empty and have been disregarded by using the switch '-DataOnly'. The property names have been named with the values provided with the parameter '-HeaderName'. Row number 1 with <20>Chuck Norris<69> has not been imported, because we started the import from row 2 with the parameter <20>-TopRow 2<>.
.LINK
https://github.com/dfinke/ImportExcel
.NOTES
2017/08/16 Added parameter sets for proper parameter validation and to make sure '-NoHeader' and '-HeaderRow' aren't used together
Added try/catch clause, CmdLetBinding and verbose messages
Renamed 'HeaderRow' to 'TopRow' to avoid confusion with other parameters
Renamed '-Header' to '-HeaderName'
Added test for duplicate property names
Added test for empty worksheet
Added test for no data after TopRow
Fixed incorrect import when there's no value in the first column
Fixed values being imported under the wrong property name in case one
Fixed incorrect import in case column A is empty and B and C not ( '$Worksheet.Dimension.Columns' is unreliable because it will say 2 columns are in use while it should say 3).
(Ex. Add data in cell B2 and C2, use the '-NoHeader' switch, notice P1 and P2 are incorrectly blanc.)
#>
Param(
[CmdLetBinding(DefaultParameterSetName)]
Param (
[Alias('FullName')]
[Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
$Path,
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
[String]$Path,
[Parameter(Mandatory)]
[Alias('Sheet')]
$WorkSheetname=1,
[int]$HeaderRow=1,
[string[]]$Header,
[switch]$NoHeader,
[switch]$DataOnly
[String]$WorksheetName,
[Parameter(ParameterSetName='B', Mandatory)]
[String[]]$HeaderName,
[Parameter(ParameterSetName='C', Mandatory)]
[Switch]$NoHeader,
[ValidateRange(1, 9999)]
[Int]$TopRow,
[Switch]$DataOnly
)
Process {
Begin {
Function Add-Property {
<#
.SYNOPSIS
Add the property name and value to the hashtable that will create a new object for each row.
#>
$Path = (Resolve-Path $Path).ProviderPath
write-debug "target excel file $Path"
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite"
$xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream
$workbook = $xl.Workbook
$worksheet=$workbook.Worksheets[$WorkSheetname]
$dimension=$worksheet.Dimension
$Rows=$dimension.Rows
$Columns=$dimension.Columns
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
}
Param (
[Parameter(Mandatory)]
[String]$Name,
$Value
)
Try {
$NewRow.$Name = $Value
Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$Name' and value '$Value'"
}
else {
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) {
$Header = foreach ($Column in 1..$Columns) {
$worksheet.Cells[$HeaderRow,$Column].Value
}
}
if ($Rows -eq 1) {
$Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h}
}
else {
if ($DataOnly) {
$CellsWithValues = $worksheet.Cells | where {$_.Value -and ($_.End.Row -ne 1)}
$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) {
$h=[Ordered]@{}
foreach ($Column in 0..($Columns-1)) {
if($Header[$Column].Length -gt 0) {
$Name = $Header[$Column]
$h.$Name = $worksheet.Cells[$Row,($Column+1)].Value
}
}
[PSCustomObject]$h
}
}
Catch {
throw "Failed adding the property name '$Name' with value '$Value': $_"
}
}
$stream.Close()
$stream.Dispose()
$xl.Dispose()
$xl = $null
Function Get-PropertyNames {
<#
.SYNOPSIS
Create objects containing the column number and the column name for each of the different header types.
#>
Param (
[Parameter(Mandatory)]
[Int[]]$Columns,
[Parameter(Mandatory)]
[Int]$TopRow
)
Try {
if ($NoHeader) {
$i = 0
foreach ($C in $Columns) {
$i++
$C | Select-Object @{N='Column'; E={$_}}, @{N='Value'; E={'P' + $i}}
}
}
elseif ($HeaderName) {
$i = 0
foreach ($H in $HeaderName) {
$H | Select-Object @{N='Column'; E={$Columns[$i]}}, @{N='Value'; E={$H}}
$i++
}
}
else {
if ($TopRow -eq 0) {
throw 'The top row can never be equal to 0 when we need to retrieve headers from the worksheet.'
}
foreach ($C in $Columns) {
$Worksheet.Cells[$TopRow,$C] | where {$_.Value} | Select-Object @{N='Column'; E={$C}}, Value
}
}
}
Catch {
throw "Failed creating property names: $_"
}
}
}
Process {
Try {
$Path = (Resolve-Path $Path).ProviderPath
Write-Verbose "Import Excel workbook '$Path' with worksheet '$Worksheetname'"
$Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite'
$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream
if (-not ($Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName])) {
throw "Worksheet '$WorksheetName' not found, the workbook only contains the worksheets '$($Excel.Workbook.Worksheets)'."
}
if (-not $Worksheet.Dimension.Rows) {
Write-Warning "Worksheet '$WorksheetName' in workbook '$Path' is empty"
}
else {
#region Set the top row
if (((-not ($NoHeader -or $HeaderName)) -and ($TopRow -eq 0))) {
$TopRow = 1
}
#endregion
#region Collect all cells that contain data starting from the top row
$AllCells = $Worksheet.Cells | where {($_.Start.Row -ge $TopRow)}
if ($DataOnly) {
$CellsWithValues = $AllCells | where {$_.Value}
$Columns = $CellsWithValues.Start.Column | Sort-Object -Unique
$Rows = $CellsWithValues.Start.Row | Sort-Object -Unique
}
else {
$LastColumn = $AllCells.Start.Column | Sort-Object -Unique | Select-Object -Last 1
$Columns = 1..$LastColumn
$LastRow = $AllCells.Start.Row | Sort-Object -Unique | Select-Object -Last 1
$Rows = $TopRow..$LastRow | where {($_ -ge $TopRow) -and ($_ -gt 0)}
}
#endregion
#region Create property names
if (-not ($PropertyNames = Get-PropertyNames -Columns $Columns -TopRow $TopRow)) {
throw "No column headers found on top row '$TopRow'. If column headers in the worksheet are not a requirement then please use the '-NoHeader' or '-HeaderName' parameter."
}
if ($Duplicates = $PropertyNames | Group-Object Value | where Count -GE 2) {
throw "Duplicate column headers found on row '$TopRow' in columns '$($Duplicates.Group.Column)'. Column headers must be unique, if this is not a requirement please use the '-NoHeader' or '-HeaderName' parameter."
}
#endregion
#region Filter out rows with data in columns that don't have a column header
if ($DataOnly -and (-not $NoHeader)) {
$Rows = $CellsWithValues.Start | where {$PropertyNames.Column -contains $_.Column} |
Sort-Object Row -Unique | Select-Object -ExpandProperty Row
}
#endregion
#region Filter out the top row when it contains column headers
if (-not ($NoHeader -or $HeaderName)) {
$Rows = $Rows | where {$_ -gt $TopRow}
}
#endregion
if (-not $Rows) {
Write-Warning "Worksheet '$WorksheetName' in workbook '$Path' contains no data in the rows after top row '$TopRow'"
}
else {
#region Create one object per row
foreach ($R in $Rows) {
Write-Verbose "Import row '$R'"
$NewRow = [Ordered]@{}
foreach ($P in $PropertyNames) {
Add-Property -Name $P.Value -Value $Worksheet.Cells[$R, $P.Column].Value
}
[PSCustomObject]$NewRow
}
#endregion
}
}
}
Catch {
throw "Failed importing the Excel workbook '$Path' with worksheet '$Worksheetname': $_"
}
Finally {
$Stream.Close()
$Stream.Dispose()
$Excel.Dispose()
$Excel = $null
}
}
}