如何使用dataTable对象将工作表直接读入数组,而无需打开excel文件,也无需循环遍历每个单元格

问题描述 投票:0回答:1

我使用的是vb.net 2012 v14.8.4084

这个简单的函数旨在将 Excel 文件的sheet1 中的所有内容转储到数组中

然后将此数组打印到控制台

由于第 27 行使用了“AsEnumerate”,此代码无法编译

我不确定它是否由于语法错误、声明错误、我的编译器太旧(无法更改)而失败

这是错误消息

readfromxls.vb(27) : error BC30456: 'AsEnumerable' is not a member of 'System.Data.DataTable'.

                Dim data As String() = dataTable.AsEnumerable().SelectMany(Function(row) row.ItemArray.Select(Function(item) item.ToString())).ToArray()

这是代码本身

Imports System.Data.OleDb

Module Module1

Public Function ReadWorksheetData(ByVal filePath As String, ByVal worksheetName As String) As String()
    ' Determine the connection string based on the file extension
    Dim connectionString As String
    If System.IO.Path.GetExtension(filePath).ToLower() = ".xlsx" Then
        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;';"
    Else
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';"
    End If

    ' Create a new connection
    Using connection As New OleDbConnection(connectionString)
        connection.Open()

        ' Create a command to select all data from the worksheet
        Using command As New OleDbCommand("SELECT * FROM [" & worksheetName & "$]", connection)
            ' Execute the command and get the data
            Using reader As OleDbDataReader = command.ExecuteReader()
                ' Load the data into a DataTable
                Dim dataTable As New System.Data.DataTable()
                dataTable.Load(reader)

                ' Convert the DataTable to a string array
                Dim data As String() = dataTable.AsEnumerable().SelectMany(Function(row) row.ItemArray.Select(Function(item) item.ToString())).ToArray()

                ' Return the cell data as a string array
                Return data
            End Using
        End Using
    End Using
End Function


Public Sub TestReadWorksheetData()
    ' Specify the file path and worksheet name
    Dim filePath As String = "testfile.xlsx"
    Dim worksheetName As String = "Sheet1"

    ' Call the function to read the worksheet data
    Dim data As String() = ReadWorksheetData(filePath, worksheetName)

    ' Loop through the string array and output each string to the console
    For Each str As String In data
        Console.WriteLine(str)
    Next
End Sub

public sub main()
    TestReadWorksheetData()
end sub

End Module

更正,错误消息是关于“AsEnumerable”而不是“AsEnumerate”

这是编译器错误消息

H:\ow\readfromxls>vbc /nologo readfromxls.vb
H:\ow\readfromxls\readfromxls.vb(27) : error BC30456: 'AsEnumerable' is not a member of 'System.Data.DataTable'.

                Dim data As String() = dataTable.AsEnumerable().SelectMany(Function(row) row.ItemArray.Select(Function(item) item.ToString())).ToArray()
                                       ~~~~~~~~~~~~~~~~~~~~~~
vb.net vb.net-2010
1个回答
0
投票

所讨论的函数是

AsEnumerable
,而不是
AsEnumerate
。这个想法是它生成一个
IEnumerable(Of DataRow)
,因为 LINQ 对实现
IEnumerable(Of T)
的对象进行操作。该方法是 LINQ to DataSet 提供程序的一部分,因此您需要确保您的项目具有使用该提供程序的适当引用。 Here 是 .NET Framework 4.5 该方法的文档。与所有类型/成员文档一样,它指定您需要引用哪个程序集才能访问它。在本例中,它是 System.Data.DataSetExtensions.dll,它基本上是 LINQ to DataSet 库。

© www.soinside.com 2019 - 2024. All rights reserved.