使用PowerShell搜索Excel并返回单元格地址

时间:2017-03-08 13:28:09

标签: excel powershell

我有一个PowerShell脚本,它在Excel文件中搜索服务器,然后输出服务器的详细信息。我的脚本有效但效率低(见下文)。

我想要做的是使用Excel Range.Find方法来提高速度,但我无法在PowerShell中使用正确的语法。

此外,是否可以使用PowerShell打开Excel文档,查找要搜索的值并设置焦点?这是我努力的设定焦点部分。

提前致谢

Function SearchExcel{
#Specify the path of the excel file
$FilePath = "c:\temp\Servers.xlsx"

$ServerName="DC1"
#Specify the Sheet name
$SheetName = "VMs","Physical Servers"


$Row = 1
$Column = 1
$Found = $False

# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
#$objExcel.Visible = $true
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'

$WorkSheet = $WorkBook.sheets.item($SheetName[0])
$range=$WorkSheet.Range("a1:a500")

#Search Hyper-V sheet
while (($WorkSheet.Cells.Item($Row, $Column).Value() -ne $Null) -and ($Found -eq $False)) {

                                                                                    #^-- looping though the excel list, updating the row. Stop if Cell is Empty or Value is Found
    If (($WorkSheet.Cells.Item($Row, $Column).Value()) -eq $ServerName) {
                                                                                    #^-- Cell value equals $Arg
      Write-Host $WorkSheet.Cells.Item($Row, $Column).Value() $WorkSheet.Cells.Item($Row, $Column+1).Value(), $WorkSheet.Cells.Item($Row, $Column+2).Value(), $WorkSheet.Cells.Item($Row, $Column+3).Value(),$WorkSheet.Cells.Item($Row, $Column+4).Value(),"Cluster:" $WorkSheet.Cells.Item($Row, $Column+6).Value(), "Backed up by:" $WorkSheet.Cells.Item($Row, $Column+7).Value(),$WorkSheet.Cells.Item($Row, $Column+8).Value(),$WorkSheet.Cells.Item($Row, $Column+10).Value(),"vCPUs:" $WorkSheet.Cells.Item($Row, $Column+13).Value(),"RAM (GB):" $WorkSheet.Cells.Item($Row, $Column+14).Value(),"C: (GB):" $WorkSheet.Cells.Item($Row, $Column+15).Value(),"Page (GB):" $WorkSheet.Cells.Item($Row, $Column+16).Value(), "Total Disk (GB):" $WorkSheet.Cells.Item($Row, $Column+26).value().ToString(),$WorkSheet.Cells.Item($Row, $Column+24).Value()
     # write-host "processing"
      $Found = $True
    }
    $Row += 1                                                                       #Continue to the next row
  }#while

  write-host "Not found, searching second sheet"

  #If not found, search physical servers
    If ($found -eq $False)
        {
        #Search Physical VMs sheet
        $WorkSheet = $WorkBook.sheets.item($SheetName[1])
        $range=$WorkSheet.Range("a1:a200")

        while (($WorkSheet.Cells.Item($Row, $Column).Value() -ne $Null) -and ($Found -eq $False)) {
                                                                                       #^-- looping though the excel list, updating the row. Stop if Cell is Empty or Value is Found
    If (($WorkSheet.Cells.Item($Row, $Column).Value()) -eq $ServerName) {
                                                                                    #^-- Cell value equals $Arg
      Write-Host $WorkSheet.Cells.Item($Row, $Column).Value() $WorkSheet.Cells.Item($Row, $Column+1).Value(), $WorkSheet.Cells.Item($Row, $Column+21).Value(), $WorkSheet.Cells.Item($Row, $Column+22).Value(),$WorkSheet.Cells.Item($Row, $Column+23).Value(),"Cluster:" $WorkSheet.Cells.Item($Row, $Column+29).Value()
     # write-host "processing"
      $Found = $True
    }
    $Row += 1                                                                       #Continue to the next row
  }


    }#if

#Close Excel
  $workbook.close()
  $objExcel.Quit()
 }#Function 

  SearchExcel

2 个答案:

答案 0 :(得分:0)

没关系,我通过更改线路来显着加快了这一步:

(($ WorkSheet.Cells.Item($ Row,$ Column).Value()-ne $ Null) - 和($ Found -eq $ False))

($ row -le 500)-and($ Found -eq $ False))

答案 1 :(得分:0)

PowerShell脚本不需要逐个单元格搜索文本。您可以使用Excel VBA对象的Range.Find方法。

以下代码可能对您有所帮助。

Function Search-Sheet 
{ 
    [CmdletBinding()] 
    Param 
    ( 
        [Parameter(Mandatory=$true)]$Sheet, 
        [Parameter(Mandatory=$true)][String]$SearchText 
    ) 
    $firstResult = $result = $Sheet.UsedRange.Find($SearchText) 
    $allResults = New-Object System.Collections.ArrayList 

    If ($firstResult -eq $null) { 
        Return $allResults 
    } 

    $processedResult = Compose-SearchResult -Result $result 
    $allResults.Add($processedResult) | Out-Null 

    $isSearchEnd = $false 
    Do { 
        $result = $Sheet.UsedRange.FindNext($result) 
        $isSearchEnd = Test-SearchResultSame -Result1 $result -Result2 $firstResult 
        If (-not $isSearchEnd) { 
            $processedResult = Compose-SearchResult -Result $result 
            $allResults.Add($processedResult) | Out-Null 
        } 
    } While (-not $isSearchEnd) 

    Return $allResults 
}

您也可以在此处下载完整的示例脚本How to search text in Excel by PowerShell script