简体   繁体   English

将ADODB记录集拆分为Excel工作表?

[英]Splitting ADODB Recordset to Excel worksheet?

I have a small macro program that extracts almost 2 million rows of data from SQL to Excel worksheet. 我有一个小型的宏程序,可以从SQL到Excel工作表中提取近200万行数据。 But the problem is, each worksheet can only contain up to 1048576 rows, so it cuts my data. 但是问题是,每个工作表最多只能包含1048576行,因此会削减我的数据。

I'm trying to figure out if there's a way to split the ADODB Recordset before you paste it to Excel. 我试图弄清楚是否有一种方法可以将ADODB Recordset粘贴到Excel之前。

Here's my code for extracting the data from SQL to Excel: 这是我从SQL提取数据到Excel的代码:

With oRecordSet
    .ActiveConnection = oDBConnection
    .Source = MySql
    .LockType = adLockReadOnly
    .CursorType = adOpenForwardOnly
    .Open
End With
Sheets("Data)").Range("A2").CopyFromRecordset oRecordSet

Appreciate your help guys. 感谢您的帮助。 Thanks in advance. 提前致谢。

You can query the data and apply some filtering logic . 您可以查询数据并应用一些过滤逻辑

You can try delimit , and manage up to 100 million rows. 您可以尝试delimit ,并管理多达1亿行。

Or, use a file splitting tool (like this or this ). 或者,使用文件分割工具(例如thisthis )。

You can try a VBA solution as well. 您也可以尝试使用VBA解决方案。

step1 第1步

Save As, your Workbook with extension .xlsm (macros enabled) 另存为,扩展名为.xlsm的工作簿(启用了宏)

step2 第2步

  1. press ALT + F11 to open Visual Basic ALT + F11打开Visual Basic

  2. Insert > module and paste the code below on the right (from Sub .... End Sub ) 插入>模块,然后在右侧粘贴下面的代码(来自Sub .... End Sub

Sub SplitTxt_01()

    Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
    Const N As Long = 700000  '<<< split each txt in N rows, CHANGE
    Dim myPath
    myPath = "c:\Folder1\Folder2\" '<<< folder path, CHANGE
    Dim myFile
    myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

    Dim WB As Workbook, myWB As Workbook
    Set myWB = ThisWorkbook
    Dim myWS As Worksheet
    Dim t As Long, r As Long
    Dim myStr
    Application.ScreenUpdating = False

    'split text file in separate text files
    myFile = Dir(myPath & myFile)
    Open myPath & myFile For Input As #1
    t = 1
    r = 1
    Do While Not EOF(1)
    Line Input #1, myStr
    If r > N Then
    t = t + 1
    r = 1
    End If
    Open myPath & HelperFile & t & ".txt" For Append As #2
    Print #2, myStr
    Close #2
    r = r + 1
    Loop
    Close #1

    'copy txt files in separate sheets
    For i = t To 1 Step -1
    Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
    Set WB = ActiveWorkbook
    Set rng = ActiveSheet.UsedRange
    Set myWS = myWB.Sheets.Add
    myWS.Name = HelperFile & i
    rng.Copy myWS.Cells(1, 1)
    WB.Close False
    Next
    myWB.Save

    'Delete helper txt files
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set Fldr = Fso.GetFolder(myPath)
    For Each Filename In Fldr.Files
    If Filename Like "*" & HelperFile & "*" Then Filename.Delete
    Next
    Application.ScreenUpdating = True
End Sub
  1. Press ALT + Q to Close Visual Basic ALT + Q以关闭Visual Basic

As a final thought, I'll say it may be time to move up to Python or R. 作为最后的想法,我会说是时候升级到Python或R了。

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

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