Powershell无法读取二维数组内容 [英] powershell can not read 2 dimension array content

查看:83
本文介绍了Powershell无法读取二维数组内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,我正在使用Powershell 3.6版,并希望从MS Excel工作表中读取数据。我编写了以下代码:

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:

索引[0]对访问二维数组无效。
+ $ r [0] [0]
+ ~~~~~~~~
+ CategoryInfo:InvalidOperation:(:) [],RuntimeException
+ FullyQualifiedErrorId:NeedMultiDimensionIndex

感谢您的提前帮助

推荐答案

尝试:

$r[0,0]

在我的测试中,必须从1开始才具有第一个值:

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

$r[1,1]

这篇关于Powershell无法读取二维数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆