ilogic程序通过excel给数组赋值
时间: 2023-10-04 11:07:38 浏览: 159
您可以使用ilogic中的Excel.Application COM对象来读取Excel文件中的数据并将其分配给数组。以下是一个示例ilogic程序,它使用Excel COM对象将Excel文件中的数据读入数组中:
```
'引用Microsoft Excel Object Library
Option Explicit
Dim objExcel As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim arrData() As Variant
Dim i As Integer, j As Integer
'打开Excel文件
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Temp\Sample.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)
'Read data from worksheet into array
With objWorksheet
arrData = .Range("A1:C10").Value
End With
'关闭Excel文件
objWorkbook.Close SaveChanges:=False
Set objWorkbook = Nothing
Set objExcel = Nothing
'循环遍历数组并输出
For i = LBound(arrData, 1) To UBound(arrData, 1)
For j = LBound(arrData, 2) To UBound(arrData, 2)
Debug.Print arrData(i, j)
Next j
Next i
```
在此示例中,我们首先使用CreateObject函数创建Excel.Application对象。然后,我们打开Excel文件并获取第一个工作表。接下来,我们使用Range属性将单元格范围A1:C10中的数据读入数组中。最后,我们循环遍历数组并输出其内容。请注意,我们在完成任务后关闭Excel文件并释放COM对象以释放内存。
阅读全文
相关推荐












