繁体   English   中英

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

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

我有一些适用于较小数据集的代码。 我使用的巨大数据集(800k行,25列)出现“内存不足”错误。 我试图找出一种方法来逐行将其更改为批量导出,或者可以将行组分开,而不是一次性整行。

显然它无法处理那么多数据。 我无法弄清楚如何将它分开。 有任何想法吗? 谢谢!

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

还有其他代码可以操作excel电子表格吗? 如果没有,将它写入CSV格式的平面文本文件可能会更快。 Excel将打开CSV并将其显示为普通电子表格。

暂无
暂无

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

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