简体   繁体   English

Powershell脚本在Excel中过滤特定项目

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

I have created a Powershell script to filter out a particular item in an coulmn . 我创建了一个Powershell脚本,以过滤掉库仑中的特定项目。 The script so far : 到目前为止的脚本:

$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() 

But the above code is giving error like : Exception calling "AutoFilter" with "3" argument(s): "AutoFilter method of Range class failed" 但是上面的代码给出了类似的错误:异常调用带有“ 3”参数的“ AutoFilter”:“ Range类的AutoFilter方法失败”

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

Please help me to write a Powershell script that will filter out only "Jan" & "Feb" item from a particular coulmn of an excel file . 请帮助我编写一个Powershell脚本,该脚本将仅从excel文件的特定库中过滤出“ Jan”和“ Feb”项。 Also suggest me how can I filter single or multiple item . 还建议我如何过滤单个或多个项目。

Please see my answer here, after much reaserch i have figured this out, and it works.. Howerver, i am still working on how to do the reverse and only not choose a few items...Have to give credit to the above post... But i have tested and it works great... 请在这里查看我的答案,经过大量的研究,我已经弄清楚了,并且可以正常工作。 ...但是我已经测试过了,效果很好...

Here is the working code, and a link to the thread... 这是工作代码,以及到线程的链接...

# 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 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