怎么批量搜索.xlsx文件内容:
Get-ChildItem -Recurse -Filter "*.xlsx" | ForEach-Object {
$file = $_.FullName
Write-Host "正在检查文件: $file" -ForegroundColor Yellow
try {
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Open($file)
$foundInFile = $false
foreach ($worksheet in $workbook.Worksheets) {
try {
# 搜索整个工作表
$found = $worksheet.Cells.Find("您要搜的内容")
if ($found) {
Write-Host " 找到 '您要搜的内容' 在: $($worksheet.Name)" -ForegroundColor Green
$foundInFile = $true
# 继续查找所有匹配项
$firstAddress = $found.Address
do {
Write-Host " 位置: $($found.Address)"
$found = $worksheet.Cells.FindNext($found)
} while ($found -and $found.Address -ne $firstAddress)
}
} catch {
Write-Host " 工作表 $($worksheet.Name) 搜索出错: $($_.Exception.Message)" -ForegroundColor Red
}
}
if (-not $foundInFile) {
Write-Host " 未找到 '您要搜的内容'" -ForegroundColor Gray
}
$workbook.Close($false)
$excel.Quit()
# 释放 COM 对象
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
} catch {
Write-Host "无法处理文件: $file - $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host "----------------------------------------" -ForegroundColor DarkGray
}
声明:本站内容来自公开平台,如若侵犯到您的权益,请联系我们,我们会第一时间删除!联系QQ:502428990。

