refactor: Simplify Excel file conversion by removing temporary file handling
This commit is contained in:
parent
ff2500707f
commit
64540f9a0e
@ -482,29 +482,14 @@ function Convert-Files {
|
|||||||
$workbook = $excel.Workbooks.Open($downloadPath)
|
$workbook = $excel.Workbooks.Open($downloadPath)
|
||||||
|
|
||||||
Write-Host "Saving as XLSX: $xlsxPath"
|
Write-Host "Saving as XLSX: $xlsxPath"
|
||||||
# Create a temporary filename without spaces
|
# Save directly to the final path
|
||||||
$tempFileName = [System.IO.Path]::GetRandomFileName() + ".xlsx"
|
$workbook.SaveAs([string]$xlsxPath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault)
|
||||||
$tempFilePath = Join-Path $env:TEMP $tempFileName
|
|
||||||
|
|
||||||
# Save to temp file first
|
|
||||||
$workbook.SaveAs([string]$tempFilePath, 51) # 51 = xlOpenXMLWorkbook
|
|
||||||
$workbook.Close($true)
|
$workbook.Close($true)
|
||||||
|
|
||||||
Start-Sleep -Seconds 2 # Give Excel time to finish
|
Start-Sleep -Seconds 2 # Give Excel time to finish
|
||||||
|
|
||||||
# After Excel is closed, rename the file to the desired name
|
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||||
if (Test-Path -LiteralPath $tempFilePath) {
|
throw "Failed to create XLSX file at: $xlsxPath"
|
||||||
if (Test-Path -LiteralPath $xlsxPath) {
|
|
||||||
Remove-Item -LiteralPath $xlsxPath -Force
|
|
||||||
}
|
|
||||||
Move-Item -LiteralPath $tempFilePath -Destination $xlsxPath -Force
|
|
||||||
|
|
||||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
|
||||||
throw "Failed to rename temporary file to final XLSX file"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw "Failed to create temporary XLSX file at: $tempFilePath"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||||
@ -523,11 +508,6 @@ function Convert-Files {
|
|||||||
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
||||||
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
||||||
Stop-Process -Force
|
Stop-Process -Force
|
||||||
|
|
||||||
# Clean up temp file if it still exists
|
|
||||||
if ($tempFilePath -and (Test-Path -LiteralPath $tempFilePath)) {
|
|
||||||
Remove-Item -LiteralPath $tempFilePath -Force -ErrorAction SilentlyContinue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user