mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 05:13:49 +00:00
accept a collection of paths
This commit is contained in:
@@ -3,20 +3,22 @@ function ConvertTo-ExcelXlsx {
|
||||
param
|
||||
(
|
||||
[parameter(Mandatory = $true, ValueFromPipeline)]
|
||||
[string]$Path,
|
||||
[string[]]$Path,
|
||||
[parameter(Mandatory = $false)]
|
||||
[switch]$Force
|
||||
)
|
||||
process {
|
||||
if (-Not ($Path | Test-Path) ) {
|
||||
try {
|
||||
foreach ($singlePath in $Path) {
|
||||
if (-Not ($singlePath | Test-Path) ) {
|
||||
throw "File not found"
|
||||
}
|
||||
if (-Not ($Path | Test-Path -PathType Leaf) ) {
|
||||
if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
|
||||
throw "Folder paths are not allowed"
|
||||
}
|
||||
|
||||
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
||||
$xlsFile = Get-Item -Path $Path
|
||||
$xlsFile = Get-Item -Path $singlePath
|
||||
$xlsxPath = "{0}x" -f $xlsFile.FullName
|
||||
|
||||
if ($xlsFile.Extension -ne ".xls") {
|
||||
@@ -38,12 +40,15 @@ function ConvertTo-ExcelXlsx {
|
||||
}
|
||||
}
|
||||
|
||||
if ($null -eq $Excel)
|
||||
{
|
||||
try {
|
||||
$Excel = New-Object -ComObject "Excel.Application"
|
||||
}
|
||||
catch {
|
||||
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$Excel.Visible = $false
|
||||
@@ -54,13 +59,20 @@ function ConvertTo-ExcelXlsx {
|
||||
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
|
||||
throw
|
||||
}
|
||||
finally {
|
||||
if ($null -ne $workbook) {
|
||||
$workbook.Close()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$Excel.Quit()
|
||||
$Excel = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user