2017-05-14 10 views
0

MS SQLテーブルからデータテーブルを作成した後、これをExcelに表示する方法です。それを行うためのより速い方法がありますか?データテーブルから高速データへの高速挿入

.CopyFromRecordset

シートにSQL DBからデータを取得する方法速く

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

最良の方法は、=IMPORTDATA("PUT_LINK_HERE")

されている。

私はレコードセットオプションがあることを考えます

0

Excelの相互運用機能を使用しているため、パフォーマンスを向上させる方法は、interopコールの数を最小限に抑えることです。長方形のデータブロックをExcelに転送するには、2次元の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 
関連する問題