[英]Refresh Excel connections using Powershell
我有以下代码可以打开电子表格,删除所有连接并“保存”一个新文件。
$a = New-Object -COM "Excel.Application"
$a.Visible = $false
$b = $a.Workbooks.Open("F:\Scripts\All Users.xlsx")
do
{
$b.Connections.Item(1).Delete()
$Count = $b.Connections.Count()
} until($Count -eq 0)
$b.SaveAs("F:\Scripts\Users Home Drive Search.xlsx")
$b.Close()
我想知道两件事:
RefreshAll()
是WorkBook 对象的一个方法,所以使用它来刷新外部连接:
$b.RefreshAll()
要退出 Excel 并从内存中删除使用的 COM 对象,请使用
$a.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($b)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($a)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
顺便说一句,我会使用更具描述性的变量名称,因此$excel
而不是$a
和$workbook
而不是$b
以避免以后混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.