mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Merge e1e4cfd02e into fa907da4a4
This commit is contained in:
@@ -3,21 +3,23 @@ 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
|
||||
$xlsxPath = "{0}x" -f $xlsFile.FullName
|
||||
$xlsFile = Get-Item -Path $singlePath
|
||||
$xlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
||||
|
||||
if ($xlsFile.Extension -ne ".xls") {
|
||||
throw "Expected .xls extension"
|
||||
@@ -38,25 +40,39 @@ 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
|
||||
$null = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
|
||||
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
|
||||
if ($null -eq $workbook) {
|
||||
Write-Host "Failed to open workbook"
|
||||
} else {
|
||||
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
|
||||
throw
|
||||
}
|
||||
finally {
|
||||
if ($null -ne $Excel.ActiveWorkbook) {
|
||||
$Excel.ActiveWorkbook.Close()
|
||||
if ($null -ne $workbook) {
|
||||
$workbook.Close()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$Excel.Quit()
|
||||
$Excel = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user