繁体   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