简体   繁体   English

在VB中将批量数据从SQL Server导出到Excel

[英]Mass data export from SQL Server to Excel in VB

I have some code that works for smaller data sets. 我有一些适用于较小数据集的代码。 I get an 'out of memory' error with the huge data sets I use though (800k rows, 25 columns). 我使用的巨大数据集(800k行,25列)出现“内存不足”错误。 I was trying to figure out a way to change this to mass export column by column, or maybe split up sets of rows, instead of the whole thing at once. 我试图找出一种方法来逐行将其更改为批量导出,或者可以将行组分开,而不是一次性整行。

Clearly it can't handle that much data. 显然它无法处理那么多数据。 I couldn't figure out how to separate it out some. 我无法弄清楚如何将它分开。 Any ideas? 有任何想法吗? Thanks! 谢谢!

For Each dt As System.Data.DataTable In ds.Tables
        ' Copy the DataTable to an object array
        Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object

        ' Copy the column names to the first row of the object array
        For col = 0 To dt.Columns.Count - 1
            rawData(0, col) = dt.Columns(col).ColumnName
        Next

        ' Copy the values to the object array
        For col = 0 To dt.Columns.Count - 1
            For row = 0 To dt.Rows.Count - 1
                rawData(row + 1, col) = dt.Rows(row).ItemArray(col)
            Next
        Next

        ' Calculate the final column letter
        Dim finalColLetter As String = String.Empty
        Dim colCharset As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim colCharsetLen As Integer = colCharset.Length

        If dt.Columns.Count > colCharsetLen Then
            finalColLetter = colCharset.Substring( _
             (dt.Columns.Count - 1) \ colCharsetLen - 1, 1)
        End If

        finalColLetter += colCharset.Substring( _
          (dt.Columns.Count - 1) Mod colCharsetLen, 1)

        ' Fast data export to Excel
        Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1)
        excelSheet.Range(excelRange, Type.Missing).Value2 = rawData

        excelSheet = Nothing
    Next

Is there other code that manipulates the excel spreadsheet? 还有其他代码可以操作excel电子表格吗? If not, it would probably be faster to just write this out to a plane text file in CSV format. 如果没有,将它写入CSV格式的平面文本文件可能会更快。 Excel will open the CSV and present it like a normal spreadsheet. Excel将打开CSV并将其显示为普通电子表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM