使用Powershell循环遍历Excel文件并检查是否存在电子表格名称

时间:2015-03-17 14:09:01

标签: excel powershell

我正在尝试编写一个powershell脚本,它将循环遍历给定目录中的每个excel文件,检查文件中是否有特定命名的工作表,然后将该文件复制到另一个位置(如果匹配)。

请参阅下面我已经尝试过的内容:

[void][reflection.assembly]::Loadwithpartialname("microsoft.office.excel")

$Excel = New-Object -ComObject Excel.Application
$tempLocation = "C:\Test\"     # Path to read files
$files = Get-ChildItem C:\Test

ForEach ($file in $files)
{
#Check for Worksheet named TestSheet

     $WorkBook = $Excel.Workbooks.Open($file)
     $WorkSheets = $WorkBook.WorkSheets

     foreach ($WorkSheet in $Workbook.Worksheets) {
     If ($WorkSheet.Name -eq "TestSheet")
    {$path = $tempLocation + "\" + $file
         Write "Saving $path"
         Copy-Item c:\Test\$file c:\Confirmed}
     Else {Write "$path does not contain TestSheet"}
     $WorkBook.Close()
}
}

此脚本在PowerShell中不返回任何错误,但只是坐在那里而不写任何内容或复制任何文件。有什么想法吗?

编辑:这是我现在正在成功运行的最终脚本

$ErrorActionPreference= 'silentlycontinue'
$tempLocation = "C:\Source"     # Path to read files
$targetlocation = "C:\Target"
Write "Loading Files..."
$files = Get-ChildItem C:\Source
Write "Files Loaded."
ForEach ($file in $files)
{
#Check for Worksheet named TestSheet
     $Excel = New-Object -ComObject Excel.Application
     $Excel.visible = $false
     $Excel.DisplayAlerts = $false
     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $WorkSheets = $WorkBook.WorkSheets | where {$_.name -eq "TestSheet"}

     if($WorkSheets) {
     $path = $tempLocation + "\" + $file
     $dest = $targetlocation + "\" + $file
     Write "Saving $path"
     $WorkBook.SaveAs($dest)
     }
     $Excel.Quit()
     Stop-Process -processname EXCEL
}
Read-host -prompt "The Scan has completed.  Press ENTER to close..."
clear-host;

2 个答案:

答案 0 :(得分:4)

我的脚本的逻辑有几个问题。以下脚本成功运行!花了几个小时研究......

$ErrorActionPreference= 'silentlycontinue'
$tempLocation = "C:\Source"     # Path to read files
$targetlocation = "C:\Target"
Write "Loading Files..."
$files = Get-ChildItem C:\Source
Write "Files Loaded."
ForEach ($file in $files)
{
#Check for Worksheet named TestSheet
     $Excel = New-Object -ComObject Excel.Application
     $Excel.visible = $false
     $Excel.DisplayAlerts = $false
     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $WorkSheets = $WorkBook.WorkSheets | where {$_.name -eq "TestSheet"}

     if($WorkSheets) {
     $path = $tempLocation + "\" + $file
     $dest = $targetlocation + "\" + $file
     Write "Saving $path"
     $WorkBook.SaveAs($dest)
     }
     $Excel.Quit()
     Stop-Process -processname EXCEL
}
Read-host -prompt "The Scan has completed.  Press ENTER to close..."
clear-host;

答案 1 :(得分:1)

你不需要这一行:

[void][reflection.assembly]::Loadwithpartialname("microsoft.office.excel")

($ Excel = New-Object -ComObject Excel.Application就足够了)

我不认为你引用了Excel文件的完整路径。尝试修改此行:

$WorkBook = $Excel.Workbooks.Open($file)

修改:

$WorkBook = $Excel.Workbooks.Open($file.Fullname)

此外,考虑在Get-ChildItem命令中添加一个过滤器,如果有子目录或非Excel文件,它们将导致错误:

$files = Get-ChildItem C:\Test -filter "*.xls"