From 5b2311c298716e01951a2a2167f747ac02ec5951 Mon Sep 17 00:00:00 2001 From: Francois Lachance-Guillemette Date: Fri, 21 Aug 2015 10:59:47 -0400 Subject: [PATCH 1/3] Allow reading even if file is open by Excel --- ImportExcel.psm1 | 8 ++++++-- TestImportExcel.ps1 | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 TestImportExcel.ps1 diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 2978967..83ace78 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -12,8 +12,10 @@ function Import-Excel { $FullName = (Resolve-Path $FullName).Path write-debug "target excel file $FullName" + + $stream = New-Object System.IO.FileStream $FullName,"Open","Read","ReadWrite" - $xl = New-Object OfficeOpenXml.ExcelPackage $FullName + $xl = New-Object OfficeOpenXml.ExcelPackage $stream $workbook = $xl.Workbook @@ -39,7 +41,9 @@ function Import-Excel { } [PSCustomObject]$h } - + + $stream.Close() + $stream.Dispose() $xl.Dispose() $xl = $null } diff --git a/TestImportExcel.ps1 b/TestImportExcel.ps1 new file mode 100644 index 0000000..7d9d8c7 --- /dev/null +++ b/TestImportExcel.ps1 @@ -0,0 +1,3 @@ +Import-Module "$PSScriptRoot/ImportExcel" -Force + +Import-Excel "$PSScriptRoot/test.xlsx" -ErrorAction Stop \ No newline at end of file From 434a3e5180647eeb1dd539b630e7cdc85a48acb0 Mon Sep 17 00:00:00 2001 From: Francois Lachance-Guillemette Date: Fri, 21 Aug 2015 11:18:09 -0400 Subject: [PATCH 2/3] Added Reading in ConvertFrom-ExcelSheet + ... Streamlined parameters in Import-Excel (still compatible) --- ImportExcel.psm1 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 83ace78..14a00fc 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -2,20 +2,20 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" function Import-Excel { param( - [Parameter(ValueFromPipelineByPropertyName=$true)] - $FullName, + [Alias("FullName")] + [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory)] + $Path, $Sheet=1, [string[]]$Header ) Process { - $FullName = (Resolve-Path $FullName).Path - write-debug "target excel file $FullName" + $Path = (Resolve-Path $Path).Path + write-debug "target excel file $Path" - $stream = New-Object System.IO.FileStream $FullName,"Open","Read","ReadWrite" - - $xl = New-Object OfficeOpenXml.ExcelPackage $stream + $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite" + $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream $workbook = $xl.Workbook @@ -344,7 +344,8 @@ function ConvertFrom-ExcelSheet { ) $Path = (Resolve-Path $Path).Path - $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path + $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite" + $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream $workbook = $xl.Workbook $targetSheets = $workbook.Worksheets | Where {$_.Name -like $SheetName} From 6c25606b91786a99c183d6b1bb08fba644c6b670 Mon Sep 17 00:00:00 2001 From: Francois Lachance-Guillemette Date: Fri, 21 Aug 2015 11:26:12 -0400 Subject: [PATCH 3/3] Added missing Stream Close and Alias --- ImportExcel.psm1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 14a00fc..3014122 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -328,6 +328,7 @@ function ConvertFrom-ExcelSheet { [CmdletBinding()] param ( + [Alias("FullName")] [Parameter(Mandatory = $true)] [String] $Path, @@ -363,7 +364,9 @@ function ConvertFrom-ExcelSheet { Import-Excel $Path -Sheet $($sheet.Name) | Export-Csv @params -Encoding $Encoding } - + + $stream.Close() + $stream.Dispose() $xl.Dispose() }