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)
|
||||
|
||||
Write-Host "Saving as XLSX: $xlsxPath"
|
||||
# Create a temporary filename without spaces
|
||||
$tempFileName = [System.IO.Path]::GetRandomFileName() + ".xlsx"
|
||||
$tempFilePath = Join-Path $env:TEMP $tempFileName
|
||||
|
||||
# Save to temp file first
|
||||
$workbook.SaveAs([string]$tempFilePath, 51) # 51 = xlOpenXMLWorkbook
|
||||
# Save directly to the final path
|
||||
$workbook.SaveAs([string]$xlsxPath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault)
|
||||
$workbook.Close($true)
|
||||
|
||||
Start-Sleep -Seconds 2 # Give Excel time to finish
|
||||
|
||||
# After Excel is closed, rename the file to the desired name
|
||||
if (Test-Path -LiteralPath $tempFilePath) {
|
||||
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"
|
||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||
throw "Failed to create XLSX file at: $xlsxPath"
|
||||
}
|
||||
|
||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||
@ -523,11 +508,6 @@ function Convert-Files {
|
||||
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
||||
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"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user