简体   繁体   English

Excel ADODB 连接到 csv 不返回所有记录

[英]Excel ADODB connection to csv not returning all records

I'm using Excel VBA code to connect to a CSV file (24,179,689 rows) via ADODB connection.我正在使用 Excel VBA 代码通过 ADODB 连接连接到 CSV 文件(24,179,689 行)。 The macro runs and gets data from the excel with a specific filter on one column.宏运行并从 excel 中获取数据,其中一列上有特定的过滤器。 With this filter that I'm trying now, it should return about 1500 rows of data.使用我现在正在尝试的这个过滤器,它应该返回大约 1500 行数据。

I have checked this by loading the CSV manually somewhere else, and the data is actually there.我已经通过在其他地方手动加载 CSV 来检查这一点,数据实际上就在那里。 But when I use the ADODB connection, my recordset remains empty.但是当我使用 ADODB 连接时,我的记录集仍然是空的。

I did some extra test: count(*) on the complete CSV file, and there I see the error: it only returns 155,535 rows.我做了一些额外的测试:count(*) 对完整的 CSV 文件,我看到了错误:它只返回 155,535 行。 So probably the specific filter that I'm applying is not in that data and therefore it returns 0 rows.因此,我正在应用的特定过滤器可能不在该数据中,因此它返回 0 行。

This is my code:这是我的代码:

Public adoConn As ADODB.Connection
Public adoRS As ADODB.Recordset

Sub getdata()

Set adoConn = New ADODB.Connection
Set adoRS = New ADODB.Recordset

Dim rawFile As String
Dim strSQL As String

'The xlsx file to treat as a database
rawFile = "myPathName"

'Open a connection to the workbook
sconnect = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & rawFile & ";Extended Properties='text;HDR=YES;FMT=Delimited'"

'Write the SQL necessary to get the data you want 
sql2 = "SELECT count(*) from [MyFileName.csv]"

'Now we open up our recordset using the connection and the sql statement
adoRS.Open sql2, adoConn, adOpenStatic

Debug.Print (adoRS.EOF)

'Last, we dump the results in this viz sheet
Blad1.Range("A1").CopyFromRecordset adoRS

adoRS.Close
adoConn.Close

End Sub

So then it returns the 155,535.所以它返回 155,535。

I also tried by creating a ADODB command and not using the connection as above.我还尝试通过创建 ADODB 命令而不使用上述连接。 Or with connection timeouts.或连接超时。 No results.没有结果。

Is this a memory issue or something else?这是 memory 问题还是其他问题? How can it be solved?如何解决?

Please try this and see if it does what you want.请尝试一下,看看它是否符合您的要求。 Also, set a Reference to Microsoft ActiveX Data Objects 2.8 Library.此外,设置对 Microsoft ActiveX 数据对象 2.8 库的引用。

Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray

Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset

Dim DBPath As String, sconnect As String

DBPath = "C:\your_path_here\"

sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & ";Extended Properties='text;HDR=YES;FMT=Delimited'"

Conn.Open sconnect
    sSQLSting = "SELECT * From CSV1.csv WHERE ID = 2"
    mrs.Open sSQLSting, Conn
        ActiveSheet.Range("A2").CopyFromRecordset mrs
    'Close Recordset
    mrs.Close

Conn.Close

My CSV looks like this.我的 CSV 看起来像这样。

在此处输入图像描述

I have a little over a million rows in my CSV;我的 CSV 中有超过一百万行; I can't put in 23 million rows, Anyway, in my test, with just over a million rows, I got the exact results that I expected, in about 1 second, and my computer is super-super-super-slow!!我不能输入 2300 万行,无论如何,在我的测试中,只有超过 100 万行,我得到了我预期的确切结果,大约 1 秒,我的电脑超级超级超级慢!!

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

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