在vb.net中将整列从Excel复制到数组列表的方法如下:
Dim excelApp As New Excel.Application
Dim workbook As Excel.Workbook = excelApp.Workbooks.Open("Excel文件路径")
Dim worksheet As Excel.Worksheet = workbook.Worksheets("工作表名称")
Dim range As Excel.Range = worksheet.Range("列的范围")
Dim values As Object(,) = range.Value
Dim arrayList As New ArrayList
For i As Integer = 1 To values.GetLength(0)
arrayList.Add(values(i, 1))
Next
workbook.Close()
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
完整的代码示例:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim excelApp As New Excel.Application
Dim workbook As Excel.Workbook = excelApp.Workbooks.Open("Excel文件路径")
Dim worksheet As Excel.Worksheet = workbook.Worksheets("工作表名称")
Dim range As Excel.Range = worksheet.Range("列的范围")
Dim values As Object(,) = range.Value
Dim arrayList As New ArrayList
For i As Integer = 1 To values.GetLength(0)
arrayList.Add(values(i, 1))
Next
workbook.Close()
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
' 使用数组列表中的数据进行后续操作
For Each item As Object In arrayList
' 处理每个值
Next
End Sub
End Class
请注意,上述代码仅适用于使用Microsoft Office安装的计算机。如果你的计算机上没有安装Microsoft Office,你可以考虑使用第三方库,如EPPlus或NPOI,来处理Excel文件。这些库提供了更简单和灵活的方法来读取和写入Excel文件。
领取专属 10元无门槛券
手把手带您无忧上云