Powershell – 方法调用失败,因为SystemComObject不包含名为单元格的方法

我一直在试图执行下面的代码来从Excel电子表格中提取信息,但是它保持失败,出现以下错误:

Method invocation failed because [System.__ComObject#{000208d8-0000-0000-c000-000000000046}] doesn't contain a method named 'cells'. At run.ps1:42 char:52 + for ($row = [int]$matches[1] + 1; $sh.cells <<<< ($row, 2) -ne ""; $row++) + CategoryInfo : InvalidOperation: (cells:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound 

任何想法如何解决这个问题? 这是我的代码:

 $(for ($i=3; $i -le $wb.sheets.count; $i++){ $sh=$wb.Sheets.Item($i) $startCell_appname = $sh.cells.item(1,2).EntireColumn.find("Application Name").Address() if ($startCell_appname -match '\$\w+\$(\d+)') { for ($row = [int]$matches[1] + 1; $sh.Cells($row, 2) -ne ""; $row++) { $apps = "" | Select appname,location,lob,os $apps.appname = $sh.Cells($row, 2).Value2 # I don't know if the location value is in column 3; this is just an example. $apps.location = $sh.Cells($row, 3).Value2 $apps.lob = $sh.Name $apps.os = "Windows 7" $apps } } }) | Export-csv "apps.csv" -NoTypeInformation 

使用$sh.Cells.Item($row,2)就像你在分配$startCell_appname