mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-15 15:53:32 +00:00
Merge pull request #35 from francoislg/master
Allow reading a file already open in Excel
This commit is contained in:
@@ -2,18 +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"
|
||||
|
||||
$xl = New-Object OfficeOpenXml.ExcelPackage $FullName
|
||||
$Path = (Resolve-Path $Path).Path
|
||||
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
|
||||
|
||||
@@ -39,7 +41,9 @@ function Import-Excel {
|
||||
}
|
||||
[PSCustomObject]$h
|
||||
}
|
||||
|
||||
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
$xl.Dispose()
|
||||
$xl = $null
|
||||
}
|
||||
@@ -324,6 +328,7 @@ function ConvertFrom-ExcelSheet {
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Alias("FullName")]
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$Path,
|
||||
@@ -340,7 +345,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}
|
||||
@@ -358,7 +364,9 @@ function ConvertFrom-ExcelSheet {
|
||||
|
||||
Import-Excel $Path -Sheet $($sheet.Name) | Export-Csv @params -Encoding $Encoding
|
||||
}
|
||||
|
||||
|
||||
$stream.Close()
|
||||
$stream.Dispose()
|
||||
$xl.Dispose()
|
||||
}
|
||||
|
||||
|
||||
3
TestImportExcel.ps1
Normal file
3
TestImportExcel.ps1
Normal file
@@ -0,0 +1,3 @@
|
||||
Import-Module "$PSScriptRoot/ImportExcel" -Force
|
||||
|
||||
Import-Excel "$PSScriptRoot/test.xlsx" -ErrorAction Stop
|
||||
Reference in New Issue
Block a user