From b93c8749d9041f9bfe5c0ff55c72e83ef652807f Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:44:44 +0100 Subject: [PATCH 1/8] Added help Added help text for 'Import-Excel' --- ImportExcel.psm1 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 6ec2b28..72a7a99 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -37,6 +37,38 @@ if($PSVersionTable.PSVersion.Major -ge 5) { function Import-Excel { + <# + .SYNOPSIS + Read the content of an Excel sheet. + + .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. + + .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 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' + + .LINK + https://github.com/dfinke/ImportExcel + #> param( [Alias("FullName")] [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] From 1ee1396a561cdedd6588d2c2bd688067815e5d5d Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:48:16 +0100 Subject: [PATCH 2/8] Added 'DataOnly' parameter Added 'DataOnly' parameter: When used we will only generate objects for rows that contain text values, not for empty rows or columns. --- ImportExcel.psm1 | 66 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 72a7a99..756362f 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -78,7 +78,8 @@ function Import-Excel { $WorkSheetname=1, [int]$HeaderRow=1, [string[]]$Header, - [switch]$NoHeader + [switch]$NoHeader, + [switch]$DataOnly ) Process { @@ -98,14 +99,33 @@ function Import-Excel { $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 - } + if ($DataOnly) { + $CellsWithValues = $worksheet.Cells | where Value - [PSCustomObject]$newRow + $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)) { + $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) { @@ -116,16 +136,36 @@ function Import-Excel { if($Rows -eq 1) { $Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h} - } else { - foreach ($Row in ($HeaderRow+1)..$Rows) { - $h=[Ordered]@{} - foreach ($Column in 0..($Columns-1)) { + } + 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) { + $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 + [PSCustomObject]$h + } } } } From 782c027960cd283f627d3315a89c44818e8ef2ac Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:50:39 +0100 Subject: [PATCH 3/8] Revert "Added 'DataOnly' parameter" This reverts commit 1ee1396a561cdedd6588d2c2bd688067815e5d5d. --- ImportExcel.psm1 | 64 +++++++++--------------------------------------- 1 file changed, 12 insertions(+), 52 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 756362f..72a7a99 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -78,8 +78,7 @@ function Import-Excel { $WorkSheetname=1, [int]$HeaderRow=1, [string[]]$Header, - [switch]$NoHeader, - [switch]$DataOnly + [switch]$NoHeader ) Process { @@ -99,33 +98,14 @@ function Import-Excel { $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 + 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 } - } - 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 - } + [PSCustomObject]$newRow } } else { if(!$Header) { @@ -136,36 +116,16 @@ function Import-Excel { if($Rows -eq 1) { $Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h} - } - 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) { - $h=[Ordered]@{} - foreach ($Column in 0..($Columns-1)) { + } 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 - } + [PSCustomObject]$h } } } From 9790cb958c63e53c28aadf562fa5f67505dc549e Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:51:13 +0100 Subject: [PATCH 4/8] Revert "Revert "Added 'DataOnly' parameter"" This reverts commit 782c027960cd283f627d3315a89c44818e8ef2ac. --- ImportExcel.psm1 | 66 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 72a7a99..756362f 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -78,7 +78,8 @@ function Import-Excel { $WorkSheetname=1, [int]$HeaderRow=1, [string[]]$Header, - [switch]$NoHeader + [switch]$NoHeader, + [switch]$DataOnly ) Process { @@ -98,14 +99,33 @@ function Import-Excel { $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 - } + if ($DataOnly) { + $CellsWithValues = $worksheet.Cells | where Value - [PSCustomObject]$newRow + $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)) { + $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) { @@ -116,16 +136,36 @@ function Import-Excel { if($Rows -eq 1) { $Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h} - } else { - foreach ($Row in ($HeaderRow+1)..$Rows) { - $h=[Ordered]@{} - foreach ($Column in 0..($Columns-1)) { + } + 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) { + $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 + [PSCustomObject]$h + } } } } From d830278cbc82cb5c326b2e761302cbc7f49913eb Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:58:01 +0100 Subject: [PATCH 5/8] Improved ' Get-ExcelSheetInfo ' Added options to retrieve information about the sheet or about the whole workbook. Usefull in case you want to know who last saved the file or other information. --- Get-ExcelSheetInfo.ps1 | 70 ++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/Get-ExcelSheetInfo.ps1 b/Get-ExcelSheetInfo.ps1 index e60e9e5..1e9f4eb 100644 --- a/Get-ExcelSheetInfo.ps1 +++ b/Get-ExcelSheetInfo.ps1 @@ -1,16 +1,19 @@ Function Get-ExcelSheetInfo { - <# - .SYNOPSIS + <# + .SYNOPSIS Get worksheet names and their indices of an Excel workbook. - - .DESCRIPTION + + .DESCRIPTION The Get-ExcelSheetInfo cmdlet gets worksheet names and their indices of an Excel workbook. - + .PARAMETER Path Specifies the path to the Excel file. This parameter is required. + .PARAMETER Type + Specifies which information to get, the one from the workbook or the one from the sheets. + .EXAMPLE - Get-ExcelSheetInfo .\Test.xlsx + Get-ExcelSheetInfo .\Test.xlsx .NOTES CHANGELOG @@ -18,27 +21,48 @@ Function Get-ExcelSheetInfo { .LINK https://github.com/dfinke/ImportExcel - - #> - + #> + [CmdletBinding()] - param( + Param ( [Alias("FullName")] - [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] - $Path + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)] + [String]$Path, + [ValidateSet('Sheets', 'Workbook')] + [String]$Type = 'Workbook' ) - process { - $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 - if($workbook -and $workbook.Worksheets) { - $workbook.Worksheets | - Select-Object -Property name,index,hidden,@{ - Label = "Path" - Expression = {$Path} + + Process { + Try { + $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 + + Switch ($Type) { + 'Workbook' { + if ($workbook) { + $workbook.Properties + } } + 'Sheets' { + if ($workbook -and $workbook.Worksheets) { + $workbook.Worksheets | + Select-Object -Property name,index,hidden,@{ + Label = "Path" + Expression = {$Path} + } + } + } + Default { + Write-Error 'Unrecogrnized type' + } + } + } + Catch { + throw "Failed retrieving Excel sheet information for '$Path': $_" } } } \ No newline at end of file From ecd2fbbc1f64bc0b40ad2c31e692a79cbae508fc Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Tue, 14 Feb 2017 08:28:00 +0100 Subject: [PATCH 6/8] Revert "Improved ' Get-ExcelSheetInfo '" This reverts commit d830278cbc82cb5c326b2e761302cbc7f49913eb. --- Get-ExcelSheetInfo.ps1 | 70 ++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 47 deletions(-) diff --git a/Get-ExcelSheetInfo.ps1 b/Get-ExcelSheetInfo.ps1 index 1e9f4eb..e60e9e5 100644 --- a/Get-ExcelSheetInfo.ps1 +++ b/Get-ExcelSheetInfo.ps1 @@ -1,19 +1,16 @@ Function Get-ExcelSheetInfo { - <# - .SYNOPSIS + <# + .SYNOPSIS Get worksheet names and their indices of an Excel workbook. - - .DESCRIPTION + + .DESCRIPTION The Get-ExcelSheetInfo cmdlet gets worksheet names and their indices of an Excel workbook. - + .PARAMETER Path Specifies the path to the Excel file. This parameter is required. - .PARAMETER Type - Specifies which information to get, the one from the workbook or the one from the sheets. - .EXAMPLE - Get-ExcelSheetInfo .\Test.xlsx + Get-ExcelSheetInfo .\Test.xlsx .NOTES CHANGELOG @@ -21,48 +18,27 @@ Function Get-ExcelSheetInfo { .LINK https://github.com/dfinke/ImportExcel - #> - + + #> + [CmdletBinding()] - Param ( + param( [Alias("FullName")] - [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)] - [String]$Path, - [ValidateSet('Sheets', 'Workbook')] - [String]$Type = 'Workbook' + [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] + $Path ) - - Process { - Try { - $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 - - Switch ($Type) { - 'Workbook' { - if ($workbook) { - $workbook.Properties - } + process { + $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 + if($workbook -and $workbook.Worksheets) { + $workbook.Worksheets | + Select-Object -Property name,index,hidden,@{ + Label = "Path" + Expression = {$Path} } - 'Sheets' { - if ($workbook -and $workbook.Worksheets) { - $workbook.Worksheets | - Select-Object -Property name,index,hidden,@{ - Label = "Path" - Expression = {$Path} - } - } - } - Default { - Write-Error 'Unrecogrnized type' - } - } - } - Catch { - throw "Failed retrieving Excel sheet information for '$Path': $_" } } } \ No newline at end of file From 7cdf40e0c863142b96f07591a7f382d7d8dd3c5b Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Tue, 14 Feb 2017 09:13:46 +0100 Subject: [PATCH 7/8] Added 'Get-ExcelWorkbookInfo' --- Export-Excel.ps1 | 25 +++++++------- Get-ExcelSheetInfo.ps1 | 16 ++++++--- Get-ExcelWorkbookInfo.ps1 | 68 +++++++++++++++++++++++++++++++++++++++ ImportExcel.psm1 | 1 + 4 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 Get-ExcelWorkbookInfo.ps1 diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 2df98bb..b4ef10b 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -1,17 +1,18 @@ function Export-Excel { <# - .Synopsis - .Example - gsv | Export-Excel .\test.xlsx - .Example - ps | Export-Excel .\test.xlsx -show\ - .Example - ps | Export-Excel .\test.xlsx -WorkSheetname Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM - .Example - ps | Export-Excel .\test.xlsx -WorkSheetname Processes -ChartType PieExploded3D -IncludePivotChart -IncludePivotTable -Show -PivotRows Company -PivotData PM - .Example - Remove-Item "c:\temp\test.xlsx" -ErrorAction Ignore - Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'} + .SYNOPSIS + Export data to an Excel work sheet. + .EXAMPLE + gsv | Export-Excel .\test.xlsx + .EXAMPLE + ps | Export-Excel .\test.xlsx -show\ + .EXAMPLE + ps | Export-Excel .\test.xlsx -WorkSheetname Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM + .EXAMPLE + ps | Export-Excel .\test.xlsx -WorkSheetname Processes -ChartType PieExploded3D -IncludePivotChart -IncludePivotTable -Show -PivotRows Company -PivotData PM + .EXAMPLE + Remove-Item "c:\temp\test.xlsx" -ErrorAction Ignore + Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'} #> param( #[Parameter(Mandatory=$true)] diff --git a/Get-ExcelSheetInfo.ps1 b/Get-ExcelSheetInfo.ps1 index e60e9e5..6ab93f5 100644 --- a/Get-ExcelSheetInfo.ps1 +++ b/Get-ExcelSheetInfo.ps1 @@ -23,22 +23,28 @@ Function Get-ExcelSheetInfo { [CmdletBinding()] param( - [Alias("FullName")] + [Alias('FullName')] [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] $Path ) process { $Path = (Resolve-Path $Path).ProviderPath - write-debug "target excel file $Path" - $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite" + + $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,'Open','Read','ReadWrite' $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream $workbook = $xl.Workbook - if($workbook -and $workbook.Worksheets) { + + if ($workbook -and $workbook.Worksheets) { $workbook.Worksheets | Select-Object -Property name,index,hidden,@{ - Label = "Path" + Label = 'Path' Expression = {$Path} } } + + $stream.Close() + $stream.Dispose() + $xl.Dispose() + $xl = $null } } \ No newline at end of file diff --git a/Get-ExcelWorkbookInfo.ps1 b/Get-ExcelWorkbookInfo.ps1 new file mode 100644 index 0000000..acd2e92 --- /dev/null +++ b/Get-ExcelWorkbookInfo.ps1 @@ -0,0 +1,68 @@ +Function Get-ExcelWorkbookInfo { + <# + .SYNOPSIS + Retrieve information of an Excel workbook. + + .DESCRIPTION + The Get-ExcelWorkbookInfo cmdlet retrieves information (LastModifiedBy, LastPrinted, Created, Modified, ...) fron an Excel workbook. These are the same details that are visible in Windows Explorer when right clicking the Excel file, selecting Properties and check the Details tabpage. + + .PARAMETER Path + Specifies the path to the Excel file. This parameter is required. + + .EXAMPLE + Get-ExcelWorkbookInfo .\Test.xlsx + + CorePropertiesXml : #document + Title : + Subject : + Author : Konica Minolta User + Comments : + Keywords : + LastModifiedBy : Bond, James (London) GBR + LastPrinted : 2017-01-21T12:36:11Z + Created : 17/01/2017 13:51:32 + Category : + Status : + ExtendedPropertiesXml : #document + Application : Microsoft Excel + HyperlinkBase : + AppVersion : 14.0300 + Company : Secret Service + Manager : + Modified : 10/02/2017 12:45:37 + CustomPropertiesXml : #document + + .NOTES + CHANGELOG + 2016/01/07 Added Created by Johan Akerstrom (https://github.com/CosmosKey) + + .LINK + https://github.com/dfinke/ImportExcel + #> + + [CmdletBinding()] + Param ( + [Alias('FullName')] + [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] + [String]$Path + ) + + Process { + Try { + $Path = (Resolve-Path $Path).ProviderPath + + $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,'Open','Read','ReadWrite' + $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream + $workbook = $xl.Workbook + $workbook.Properties + + $stream.Close() + $stream.Dispose() + $xl.Dispose() + $xl = $null + } + Catch { + throw "Failed retrieving Excel workbook information for '$Path': $_" + } + } +} diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 756362f..1f0dce9 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -13,6 +13,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\New-PSItem.ps1 . $PSScriptRoot\Pivot.ps1 . $PSScriptRoot\Get-ExcelSheetInfo.ps1 +. $PSScriptRoot\Get-ExcelWorkbookInfo.ps1 . $PSScriptRoot\Get-HtmlTable.ps1 . $PSScriptRoot\Import-Html.ps1 . $PSScriptRoot\Get-Range.ps1 From e68a5f27e57fcd7af2b96216a6e59238aa6a063f Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Tue, 14 Feb 2017 09:38:08 +0100 Subject: [PATCH 8/8] Fixed 'DataOnly' header issue This has been fixed: Looks like it adds the "headers" in the sheet to the data when you specify -DataOnly --- ImportExcel.psm1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 1f0dce9..f173fa8 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -99,7 +99,7 @@ function Import-Excel { $Rows=$dimension.Rows $Columns=$dimension.Columns - if($NoHeader) { + if ($NoHeader) { if ($DataOnly) { $CellsWithValues = $worksheet.Cells | where Value @@ -128,19 +128,20 @@ function Import-Excel { [PSCustomObject]$newRow } } - } else { - if(!$Header) { + } + else { + if (!$Header) { $Header = foreach ($Column in 1..$Columns) { $worksheet.Cells[$HeaderRow,$Column].Value } } - if($Rows -eq 1) { + if ($Rows -eq 1) { $Header | ForEach {$h=[Ordered]@{}} {$h.$_=''} {[PSCustomObject]$h} } else { if ($DataOnly) { - $CellsWithValues = $worksheet.Cells | where Value + $CellsWithValues = $worksheet.Cells | where {$_.Value -and ($_.End.Row -ne 1)} $Script:i = -1 $ColumnReference = $CellsWithValues | Select-Object -ExpandProperty End | Group-Object Column |