将数据表放入新工作表

Put datatable in new worksheet

我正在尝试使用 VB 将数据 table 导入 Excel。我找到了很多方法,但所有方法都对我有问题。

我尝试的第一个方法是使用 ClosedXML。它非常适合导入数据,但它弄乱了枢轴 table。这是一个已知的问题。此外,如果数据table 太大,它会抛出内存不足异常,这是另一个已知问题。

然后我切换到Interop。但是使用 Interop 我只能找到一种导入数据的方法,那就是使用双循环,如下所示。我这里的问题是,导入 3k 行、39 列 table 需要超过 15 分钟。无论如何,这种方法对我来说似乎又慢又低效,我无法想象微软没有实施更有效的方法来导入更大的数据tables。

有没有人可以告诉我更好的方法?

For i As Integer = 0 To datatable.Rows.Count() - 1
  For j As Integer = 0 To datatable.Columns.Count() - 1
    xlWorkSheet.Cells(i + 2, j + 1) = datatable.Rows(i)(j).ToString()
  Next
Next

Fadi 的回复和快速 google 搜索为我解决了这个问题。

    'dt is your Datatable and xlWorksheet is Your Worksheet
    Dim cc = dt.Columns.Count
    Dim rc = dt.Rows.Count        

    Dim arr(rc - 1, cc - 1) As Object
    For r = 0 To rc - 1
        Dim dr = dt.Rows(r)
        For c = 0 To cc - 1
            arr(r, c) = dr(c).ToString 'I added ToString() here, because it raised an type error
        Next
    Next
    xlWorkSheet.Range("A2").Resize(rc, cc).Value = arr

逐个单元格填充 Excel 范围很慢,您可以创建二维数组并从数据表中填充它,然后将此数组分配给 Excel 范围。

我在 3000 行 39 列的 Datatable 上测试这段代码需要 670 毫秒。

    'dt is your Datatable and xlWorksheet is Your Worksheet
    Dim cc = dt.Columns.Count
    Dim rc = dt.Rows.Count        

    Dim arr(rc - 1, cc - 1) As Object
    For r = 0 To rc - 1
        Dim dr = dt.Rows(r)
        For c = 0 To cc - 1
            arr(r, c) = dr(c)
        Next
    Next
    xlWorkSheet.Range("A2").Resize(rc, cc).Value = arr

测试:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim xl = New Excel.Application
    Dim wb = xl.Workbooks.Add()
    Dim xlWorkSheet As Excel.Worksheet = CType(wb.Worksheets(1), Excel.Worksheet)
    Dim dt = CreateDatatable()

    Dim cc = dt.Columns.Count
    Dim rc = dt.Rows.Count
    Dim tt = Now.Ticks

    Dim arr(rc - 1, cc - 1) As Object
    For r = 0 To rc - 1
        Dim dr = dt.Rows(r)
        For c = 0 To cc - 1
            arr(r, c) = dr(c)
        Next
    Next
    xlWorkSheet.Range("A2").Resize(rc, cc).Value = arr
    Dim ee = (Now.Ticks - tt) / 10000
    MsgBox(ee)
    xl.Visible = True
    xl.UserControl = True

End Sub

Function CreateDatatable() As DataTable

    Dim dt = New DataTable
    For c = 1 To 39
        dt.Columns.Add($"Col{c}")
    Next
    For r = 1 To 3000
        Dim dr As DataRow = dt.NewRow
        For c = 1 To 39
            dr(c - 1) = $"R{r},C{c}"
        Next
        dt.Rows.Add(dr)
    Next
    Return dt

End Function