从datatable到excel的快速批量插入

从datatable到excel的快速批量插入,excel,vb.net,Excel,Vb.net,从MS SQL表填充数据表后,这是我在excel中显示数据表的方式。有没有更快的方法 我认为记录集选项: .CopyFromRecordset 要快得多吗 Private Sub ExportToExcel(ByVal dtTemp As DataTable, ByVal filepath As String) Dim strFileName As String = filepath Dim _excel As New Excel.Application

从MS SQL表填充数据表后,这是我在excel中显示数据表的方式。有没有更快的方法

我认为记录集选项:

.CopyFromRecordset

要快得多吗

Private Sub ExportToExcel(ByVal dtTemp As DataTable, ByVal filepath As String)
        Dim strFileName As String = filepath
        Dim _excel As New Excel.Application
        Dim wBook As Excel.Workbook
        Dim wSheet As Excel.Worksheet

        Dim newCulture As System.Globalization.CultureInfo
        Dim OldCulture As System.Globalization.CultureInfo

        OldCulture = System.Threading.Thread.CurrentThread.CurrentCulture
        newCulture = New System.Globalization.CultureInfo( _
            _excel.LanguageSettings.LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI))
        System.Threading.Thread.CurrentThread.CurrentCulture = newCulture

        wBook = _excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dtTemp
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            wSheet.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                wSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
            Next
        Next
        wSheet.Columns.AutoFit()
        wBook.SaveAs(strFileName)

        _excel.Visible = True

    End Sub

将数据从SQL DB获取到工作表中的最佳方法是
=IMPORTDATA(“此处放置链接”)

因为您使用的是Excel interop,所以提高性能的最佳方法是尽量减少interop调用的次数。通过将二维
对象
数组分配给大小相同的
Excel.Range
,可以将矩形数据块传输到Excel。根据传输的数据量,由于内存资源的消耗,单个传输或多个数据块的传输速度可能更快

以下代码以行块的形式传输数据,允许您指定每个事务传输的最大行数

Public Shared Sub ExportDTBlockMode(dt As DataTable, topLeftCell As Excel.Range, Optional maxRowsInBlock As Int32 = 1000)
    Dim calcMode As Excel.XlCalculation = topLeftCell.Application.Calculation
    topLeftCell.Application.Calculation = Excel.XlCalculation.xlCalculationManual

    Dim upperBoundRows As Int32 = Math.Min(dt.Rows.Count + 1, maxRowsInBlock) - 1

    Dim exportArray As Object(,) = New Object(0 To upperBoundRows, 0 To dt.Columns.Count - 1) {}

    Dim exportRange As Excel.Range = CType(topLeftCell.Cells.Item(1, 1), Excel.Range)
    exportRange = exportRange.Resize(upperBoundRows + 1, dt.Columns.Count)

    ' create and export header
    Dim header As New List(Of String)
    Dim colIndex As Int32
    Dim rowIndex As Int32 = 0
    Dim arrayRowIndex As Int32 = 0
    For Each c As DataColumn In dt.Columns
        exportArray(arrayRowIndex, colIndex) = c.ColumnName
        colIndex += 1
    Next

    For Each dr As DataRow In dt.Rows
        arrayRowIndex += 1
        colIndex = 0
        For Each c As DataColumn In dt.Columns
            exportArray(arrayRowIndex, colIndex) = dr.ItemArray(colIndex)
            colIndex += 1
        Next
        If arrayRowIndex = upperBoundRows Then
            exportRange.Value(Excel.XlRangeValueDataType.xlRangeValueDefault) = exportArray
            exportRange = exportRange.Offset(maxRowsInBlock, 0)
            arrayRowIndex = -1
        End If
    Next
    If arrayRowIndex > -1 Then
        Dim exportArrayResized(0 To arrayRowIndex, dt.Columns.Count - 1) As Object
        For r As Int32 = 0 To arrayRowIndex
            For c As Int32 = 0 To dt.Columns.Count - 1
                exportArrayResized(r, c) = exportArray(r, c)
            Next

        Next
        exportRange = exportRange.Resize(arrayRowIndex + 1, dt.Columns.Count)

        exportRange.Value(Excel.XlRangeValueDataType.xlRangeValueDefault) = exportArrayResized
    End If
    topLeftCell.Application.Calculation = calcMode

End Sub