簡體   English   中英

Powershell腳本在Excel中過濾特定項目

[英]Powershell script to filter a particular item in excel

我創建了一個Powershell腳本,以過濾掉庫侖中的特定項目。 到目前為止的腳本:

$file1 = "C:\Users\ab270510\Desktop\t.xlsx" # source's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb1 = $xl.workbooks.open($file1) # open target
$sh1 = $wb1.sheets.item('Sheet1') # sheet in workbook
$sh1.Select()   
$sh1.Range("C1").Select() 
$xlFilterValues = 7 # found in MS documentation
$filterList = “Jan”,”feb” # array
$xl.Selection.AutoFilter(2, $filterList ,$xlFilterValues) 

$sh1.cells.Item.EntireColumn.AutoFit 

$wb1.close($true) # close and save  workbook
$xl.quit() 

但是上面的代碼給出了類似的錯誤:異常調用帶有“ 3”參數的“ AutoFilter”:“ Range類的AutoFilter方法失敗”

At line:1 char:25
 + $xl.Selection.AutoFilter <<<< (2, $filterList ,$xlFilterValues)
 + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ComMethodTargetInvocation

請幫助我編寫一個Powershell腳本,該腳本將僅從excel文件的特定庫中過濾出“ Jan”和“ Feb”項。 還建議我如何過濾單個或多個項目。

請在這里查看我的答案,經過大量的研究,我已經弄清楚了,並且可以正常工作。 ...但是我已經測試過了,效果很好...

這是工作代碼,以及到線程的鏈接...

# Filter for an array of values IE: choose muipliple items.... in one column $xlFilterValues = 7 # found in MS documentation $FL = @("value 1", "value2") $rng=$Worksheet.cells.item(2,38).entirecolumn $rng.select | Out-Null $excel.Selection.AutoFilter(20,$FL,$xlFilterValues)

http://social.msdn.microsoft.com/Forums/office/en-US/81e4a2b0-d016-4a56-92e6-c3d4befa75db/powershell-excel-autofilter-on-multiple-items?forum=exceldev

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM