mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-15 15:53:32 +00:00
optionally cache to temp file to avoid locking issues
This commit is contained in:
@@ -5,7 +5,9 @@ function ConvertTo-ExcelXlsx {
|
|||||||
[parameter(Mandatory = $true, ValueFromPipeline)]
|
[parameter(Mandatory = $true, ValueFromPipeline)]
|
||||||
[string[]]$Path,
|
[string[]]$Path,
|
||||||
[parameter(Mandatory = $false)]
|
[parameter(Mandatory = $false)]
|
||||||
[switch]$Force
|
[switch]$Force,
|
||||||
|
[parameter(Mandatory = $false)]
|
||||||
|
[switch]$CacheToTemp
|
||||||
)
|
)
|
||||||
process {
|
process {
|
||||||
try {
|
try {
|
||||||
@@ -19,24 +21,24 @@ function ConvertTo-ExcelXlsx {
|
|||||||
|
|
||||||
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
||||||
$xlsFile = Get-Item -Path $singlePath
|
$xlsFile = Get-Item -Path $singlePath
|
||||||
$xlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
$destinationXlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
||||||
|
|
||||||
if ($xlsFile.Extension -ne ".xls") {
|
if ($xlsFile.Extension -ne ".xls") {
|
||||||
throw "Expected .xls extension"
|
throw "Expected .xls extension"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Test-Path -Path $xlsxPath) {
|
if (Test-Path -Path $destinationXlsxPath) {
|
||||||
if ($Force) {
|
if ($Force) {
|
||||||
try {
|
try {
|
||||||
Remove-Item $xlsxPath -Force
|
Remove-Item $destinationXlsxPath -Force
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
|
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $destinationXlsxPath
|
||||||
}
|
}
|
||||||
Write-Verbose $("Removed {0}" -f $xlsxPath)
|
Write-Verbose $("Removed {0}" -f $destinationXlsxPath)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw "{0} already exists!" -f $xlsxPath
|
throw "{0} already exists!" -f $destinationXlsxPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,13 +52,28 @@ function ConvertTo-ExcelXlsx {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
$tempPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetFileName($xlsFile.FullName))
|
||||||
|
Copy-Item -Path $xlsFile.FullName -Destination $tempPath -Force
|
||||||
|
$fileToProcess = $tempPath
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$fileToProcess = $xlsFile.FullName
|
||||||
|
}
|
||||||
|
|
||||||
|
$xlsxPath = [System.IO.Path]::ChangeExtension($fileToProcess, ".xlsx")
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$Excel.Visible = $false
|
$Excel.Visible = $false
|
||||||
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
|
$workbook = $Excel.Workbooks.Open($fileToProcess, $null, $true)
|
||||||
if ($null -eq $workbook) {
|
if ($null -eq $workbook) {
|
||||||
Write-Host "Failed to open workbook"
|
Write-Host "Failed to open workbook"
|
||||||
} else {
|
} else {
|
||||||
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
Copy-Item -Path $xlsxPath -Destination $destinationXlsxPath -Force
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -69,6 +86,11 @@ function ConvertTo-ExcelXlsx {
|
|||||||
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||||
$workbook = $null
|
$workbook = $null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
Remove-Item -Path $tempPath -Force
|
||||||
|
Remove-Item -Path $xlsxPath -Force
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user