Visual Basic.NET 引用 Excel 的 Range 的数组的下限值冲突
程序代码:
Imports Microsoft.Office.Interop Imports Microsoft.Office.Interop.Excel Module Module1 Sub main() Const fsiFilePath = "C:\测试.xlsx" Const ExportedRange As String = "B9:B17" Dim ExcelApp As Excel.Application ExcelApp = CreateObject("Excel.Application") ExcelApp.Visible = True Dim iWkbk As Excel.Workbook iWkbk = ExcelApp.Workbooks.Open(fsiFilePath, , True) Dim Wksht As Worksheet Wksht = iWkbk.Worksheets(1) Dim DataInRange(,) As Object DataInRange = Wksht.Range(ExportedRange).Value '从下图中可以看出来,DataInRange是从(0,0)到(8,0)的数组。 Debug.Print(LBound(DataInRange)) '返回值为1 Debug.Print(UBound(DataInRange)) '返回值为9 Dim a1 As Single = DataInRange(1, 1) '返回局部列表中(0,0)位置的值 Dim a2 = DataInRange(0, 0) '报错:索引超出了数组界限 iWkbk.Close() ExcelApp.Quit() End Sub End Module