Read-only imports + small test fix

This commit is contained in:
jhoneill
2019-06-11 15:09:36 +01:00
parent 48ca35b9ff
commit 7c2bbf9595
2 changed files with 8 additions and 5 deletions

View File

@@ -345,8 +345,11 @@ function Import-Excel {
} }
process { process {
if ($Path -and $Password) {$ExcelPackage = Open-ExcelPackage -Path $Path -Password $Password} if ($path) {
elseif ($Path) {$ExcelPackage = Open-ExcelPackage -Path $Path} $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite'
if ($Password) {$ExcelPackage = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream , $Password }
else {$ExcelPackage = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream}
}
try { try {
#Select worksheet #Select worksheet
if (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] } if (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] }
@@ -414,7 +417,7 @@ function Import-Excel {
} }
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$Worksheetname': $_"; return } catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$Worksheetname': $_"; return }
finally { finally {
if ($Path) {Close-ExcelPackage -ExcelPackage $ExcelPackage -NoSave } if ($Path) {$stream.close(); $ExcelPackage.Dispose() }
} }
} }
} }
@@ -513,7 +516,7 @@ Function WorksheetArgumentCompleter {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$xlPath = $fakeBoundParameter['Path'] $xlPath = $fakeBoundParameter['Path']
if (Test-Path -Path $xlPath) { if (Test-Path -Path $xlPath) {
$xlpkg = Open-ExcelPackage -Path $xlPath $xlpkg = Open-ExcelPackage -ReadOnly -Path $xlPath
$WorksheetNames = $xlPkg.Workbook.Worksheets.Name $WorksheetNames = $xlPkg.Workbook.Worksheets.Name
Close-ExcelPackage -nosave -ExcelPackage $xlpkg Close-ExcelPackage -nosave -ExcelPackage $xlpkg
$WorksheetNames.where( {$_ -like "*$wordToComplete*"}) | foreach-object { $WorksheetNames.where( {$_ -like "*$wordToComplete*"}) | foreach-object {

View File

@@ -1,6 +1,6 @@
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
$xlFile = ".\testSQL.xlsx" $xlFile = "$env:TEMP\testSQL.xlsx"
Describe "ConvertFrom-ExcelToSQLInsert" { Describe "ConvertFrom-ExcelToSQLInsert" {