如何正确关闭Excel.Application?
How to properly close Excel.Application?
我有一个小脚本,允许我将多个 .csv 合并到一个 .xlsx:
$path = "C:\Users\FrancescoM\Desktop\CSV\Results\*"
$csvs = Get-ChildItem $path -Include *.csv
$y = $csvs.Count
Write-Host "Detected the following CSV files: ($y)"
Write-Host " "$csvs.Name"`n"
$outputfilename = "Final Registry Results"
Write-Host Creating: $outputfilename
$excelapp = New-Object -ComObject Excel.Application
$excelapp.SheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
for ($i=1; $i -le $y; $i++) {
$worksheet = $xlsx.Worksheets.Item($i)
$worksheet.Name = $csvs[$i-1].Name
$file = (Import-Csv $csvs[$i-1].FullName)
$file | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip
$worksheet.Cells.Item(1).PasteSpecial() | Out-Null
}
$output = "Results.xlsx"
$xlsx.SaveAs($output)
$excelapp.Quit()
如果我 运行 它一旦完美运行就会创建我的“Results.xlsx”文件。
但是,如果我随后 删除 “Results.xlsx”文件并再次 运行 代码,我会收到此错误:
A file named 'Results.xlsx' already exists in this location. Do you want to replace it?
但是很明显该文件已经不存在了。我相信我以错误的方式关闭了 Excel.Application
。怎么关闭才对?
正如 Ansgar Wiechers 评论的那样,最好为这段代码使用完整路径和文件名 $output = "Results.xlsx"
否则,输出将写入 Excel 的当前目录并且可能不是你期望的那样。
要回答问题 如何正确关闭 Excel.Application?,您不仅需要在完成后退出 Excel,还需要释放 Com 对象在代码中使用。
你这样做:
$excelapp.Quit()
# release the WorkSheet Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsx) | Out-Null
# release the Excel.Application Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelapp) | Out-Null
# Force garbage collection
[System.GC]::Collect()
# Suspend the current thread until the thread that is processing the queue of finalizers has emptied that queue.
[System.GC]::WaitForPendingFinalizers()
我有一个小脚本,允许我将多个 .csv 合并到一个 .xlsx:
$path = "C:\Users\FrancescoM\Desktop\CSV\Results\*"
$csvs = Get-ChildItem $path -Include *.csv
$y = $csvs.Count
Write-Host "Detected the following CSV files: ($y)"
Write-Host " "$csvs.Name"`n"
$outputfilename = "Final Registry Results"
Write-Host Creating: $outputfilename
$excelapp = New-Object -ComObject Excel.Application
$excelapp.SheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
for ($i=1; $i -le $y; $i++) {
$worksheet = $xlsx.Worksheets.Item($i)
$worksheet.Name = $csvs[$i-1].Name
$file = (Import-Csv $csvs[$i-1].FullName)
$file | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip
$worksheet.Cells.Item(1).PasteSpecial() | Out-Null
}
$output = "Results.xlsx"
$xlsx.SaveAs($output)
$excelapp.Quit()
如果我 运行 它一旦完美运行就会创建我的“Results.xlsx”文件。
但是,如果我随后 删除 “Results.xlsx”文件并再次 运行 代码,我会收到此错误:
A file named 'Results.xlsx' already exists in this location. Do you want to replace it?
但是很明显该文件已经不存在了。我相信我以错误的方式关闭了 Excel.Application
。怎么关闭才对?
正如 Ansgar Wiechers 评论的那样,最好为这段代码使用完整路径和文件名 $output = "Results.xlsx"
否则,输出将写入 Excel 的当前目录并且可能不是你期望的那样。
要回答问题 如何正确关闭 Excel.Application?,您不仅需要在完成后退出 Excel,还需要释放 Com 对象在代码中使用。 你这样做:
$excelapp.Quit()
# release the WorkSheet Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsx) | Out-Null
# release the Excel.Application Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelapp) | Out-Null
# Force garbage collection
[System.GC]::Collect()
# Suspend the current thread until the thread that is processing the queue of finalizers has emptied that queue.
[System.GC]::WaitForPendingFinalizers()