powershell can not read 2 dimension array content

2019-09-05 13:31发布

good morning, i am using powershell version 3.6 and want to read data from an MS Excel sheet. i wrote the following code:

$objExcel = New-Object -ComObject Excel.Application
$objworkbook=$objExcel.Workbooks.Open($path)
$sheet = $objworkbook.Worksheets.Item(1)
$sheet.Visible = $true  

$r = $sheet.Range("A7:C12").value2 #$r is an 2d array
write-host $r[0][0]

$objworkbook.SaveAs($path)
$objworkbook.Close($false)
$objExcel.Application.DisplayAlerts = $False 
$objExcel.quit()
$ExcelProcess=get-process excel
$ExcelProcess | foreach {stop-process ($_.id)}

when i run this script it show me the contain of the range. but if i try get some value of the array with the index it don't work. and give this error back:

The index [0] is not valid for access to a 2-dimensional array. + $r[0][0] + ~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NeedMultidimensionalIndex

Thanks for your help in advance

1条回答
Ridiculous、
2楼-- · 2019-09-05 13:57

try:

$r[0,0]

In my test have to start from 1 to have first value:

$r[1,1]
查看更多
登录 后发表回答