Merge pull request #163 from DarkLite1/master

Fix for #162 and other improvements
This commit is contained in:
Doug Finke
2017-02-14 18:22:33 -05:00
committed by GitHub
4 changed files with 183 additions and 34 deletions

View File

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

View File

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

68
Get-ExcelWorkbookInfo.ps1 Normal file
View File

@@ -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': $_"
}
}
}

View File

@@ -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
@@ -37,6 +38,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)]
@@ -46,7 +79,8 @@ function Import-Excel {
$WorkSheetname=1,
[int]$HeaderRow=1,
[string[]]$Header,
[switch]$NoHeader
[switch]$NoHeader,
[switch]$DataOnly
)
Process {
@@ -65,35 +99,75 @@ function Import-Excel {
$Rows=$dimension.Rows
$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 ($NoHeader) {
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 {
if(!$Header) {
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) {
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 -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
[PSCustomObject]$h
}
}
}
}