VB.NET 操作Excel库MiniExcel

VB.NET · 07-03 · 378 人浏览

最近一直在找可快速读取与写入的Excel库,发现MiniExcel库非常不错。
速度也挺快。

读取:

Dim currentDirectory As String = AppDomain.CurrentDomain.BaseDirectory
Dim filePath As String = IO.Path.Combine(currentDirectory, "222.xlsx")
Dim rows As IEnumerable(Of Object) = MiniExcel.Query(filePath)


For Each row As IDictionary(Of String, Object) In rows
    If row.ContainsKey("B") Then
        Console.WriteLine($"B: {row("B")}")
    End If
Next

Console.WriteLine("读取完成")

写入(不带标头):

 Dim currentDirectory As String = AppDomain.CurrentDomain.BaseDirectory
        Dim filePath As String = Path.Combine(currentDirectory, "555.xlsx")

        ' 创建数据
        Dim data As New List(Of Object)()
        For i As Integer = 1 To 1000
            Dim row As New With {
            .A = "你好" & i,
            .B = "数据" & i,
            .C = "",
            .D = "更多数据" & i
        }
            data.Add(row)
        Next

        ' 将数据写入Excel文件,不生成表头
        MiniExcel.SaveAs(filePath, data, printHeader:=False)
        Console.WriteLine("写入完成")

写入(带标头):

 Dim currentDirectory As String = AppDomain.CurrentDomain.BaseDirectory
        Dim filePath As String = Path.Combine(currentDirectory, "555.xlsx")

        ' 创建数据
        Dim data As New List(Of Dictionary(Of String, Object))()
        For i As Integer = 1 To 1000
            Dim row As New Dictionary(Of String, Object)()
            row("A") = "你好" & i
            row("B") = "数据" & i
            row("C") = ""
            row("D") = "更多数据" & i
            data.Add(row)
        Next

        ' 将数据写入Excel文件
        MiniExcel.SaveAs(filePath, data)
        Console.WriteLine("Excel file written successfully!")

* 人工审核评论,通过后即可正常显示。软件建议请添加微信