Allow reading even if file is open by Excel

This commit is contained in:
Francois Lachance-Guillemette
2015-08-21 10:59:47 -04:00
parent 295c4a5867
commit 5b2311c298
2 changed files with 9 additions and 2 deletions

View File

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

3
TestImportExcel.ps1 Normal file
View File

@@ -0,0 +1,3 @@
Import-Module "$PSScriptRoot/ImportExcel" -Force
Import-Excel "$PSScriptRoot/test.xlsx" -ErrorAction Stop