2017-05-14 80 views
0

從MS SQL表填充數據表後,這是我在excel中顯示它的方式。有沒有更快的方法來做到這一點?從數據錶快速批量插入到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 

回答

0

從一個SQL數據庫中獲取數據表最好的辦法是=IMPORTDATA("PUT_LINK_HERE")

0

由於您使用的是Excel互操作,所以bes提高性能的方法是儘量減少互操作呼叫的數量。將矩形數據塊傳輸到Excel可以通過將二維Object陣列分配給調整大小相同的Excel.Range來完成。根據傳輸的數據量,由於內存資源的消耗,將其作爲單個傳輸或多個塊進行傳輸可能會更快。

下面的代碼傳輸中的行塊中的數據,它允許用戶指定的行的最大數量每個事務來傳輸。

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