早上好,我使用的是 powershell 3.6 版,想从 MS Excel 表中读取数据。我写了以下代码:
$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)}
当我运行此脚本时,它会显示范围的包含。但是如果我尝试使用索引获取数组的某些值,它就不起作用。并返回此错误:
索引 [0] 对访问二维数组无效。+ $r[0][0] + ~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NeedMultidimensionalIndex
提前感谢您的帮助