vb.net如何获取excel列的唯一值
时间: 2023-08-22 19:08:28 浏览: 104
获取Excel列的唯一值可以使用VB.NET的HashSet类来实现。以下是示例代码:
```
' 引用Microsoft.Office.Interop.Excel命名空间
Imports Microsoft.Office.Interop.Excel
' 打开Excel文件
Dim excelApp As New Application()
Dim excelWorkbook As Workbook = excelApp.Workbooks.Open("文件路径")
Dim excelWorksheet As Worksheet = excelWorkbook.Sheets("工作表名称")
' 获取列数据
Dim columnRange As Range = excelWorksheet.Range("列范围")
Dim columnValues As Object(,) = columnRange.Value2
' 使用HashSet获取唯一值
Dim uniqueValues As New HashSet(Of Object)()
For i As Integer = 1 To columnValues.GetLength(0)
Dim cellValue As Object = columnValues(i, 1)
If Not uniqueValues.Contains(cellValue) Then
uniqueValues.Add(cellValue)
End If
Next
' 输出唯一值
For Each uniqueValue As Object In uniqueValues
Console.WriteLine(uniqueValue)
Next
' 关闭Excel文件
excelWorkbook.Close()
excelApp.Quit()
```
阅读全文