在VB.NET中将一列的MS Excel数据放入数组中,可以通过以下步骤实现:
Dim excelApp As New Excel.Application
Dim excelWorkbook As Excel.Workbook = excelApp.Workbooks.Open("路径\文件名.xlsx")
Dim excelWorksheet As Excel.Worksheet = excelWorkbook.Sheets("工作表名称")
Dim excelRange As Excel.Range = excelWorksheet.UsedRange.Columns("列号")
Dim dataArray As Object(,) = excelRange.Value
excelWorkbook.Close()
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelRange)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
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 excelWorkbook As Excel.Workbook = excelApp.Workbooks.Open("路径\文件名.xlsx")
Dim excelWorksheet As Excel.Worksheet = excelWorkbook.Sheets("工作表名称")
Dim excelRange As Excel.Range = excelWorksheet.UsedRange.Columns("列号")
Dim dataArray As Object(,) = excelRange.Value
' 使用数组中的数据进行后续操作
excelWorkbook.Close()
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelRange)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End Sub
End Class
请注意,上述代码仅适用于VB.NET中使用Microsoft Office Interop库的情况。如果你使用的是其他库或工具,可能需要相应的修改。
领取专属 10元无门槛券
手把手带您无忧上云