Excel VBA - ADO RecordSet 返回第 2 行作为标题行而不是第 1 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9930301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:42:19  来源:igfitidea点击:

Excel VBA - ADO RecordSet returning row 2 as header row instead of row 1

excel-vbaadorecordsetvbaexcel

提问by Ashaelon

I am opening a connection to an Excel 2003 (.xls) worksheet using the following code:

我正在使用以下代码打开与 Excel 2003 (.xls) 工作表的连接:

Set adoConn = New ADODB.Connection
Set adoRS = New ADODB.Recordset
With adoConn
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & vendorSource & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    .Open
End With

adoRS.CursorLocation = adUseClient
adoRS.CursorType = adOpenStatic
adoRS.ActiveConnection = adoConn
adoRS.Open "SELECT * FROM [6000_600_VENDOR_MAIN_INFO]"

This has properly returned results from the worksheet up until recently. In the current spreadsheet that I am connecting to, the recordset is returning row 2 as the header row instead of row 1. I am not sure what has changed in the spreadsheet. Visually looking at the spreadsheet, it appears nothing is different except the cell formatting for the header row. I tried clearing the formats for row 1, but it didn't make any difference. What could be causing the recordset to return row 2 as the header row instead of row 1?

直到最近,这已正确返回工作表中的结果。在我连接到的当前电子表格中,记录集返回第 2 行作为标题行而不是第 1 行。我不确定电子表格中发生了什么变化。目视查看电子表格,除了标题行的单元格格式外,似乎没有什么不同。我尝试清除第 1 行的格式,但没有任何区别。什么可能导致记录集返回第 2 行作为标题行而不是第 1 行?

Something else to note is that I am making the connection from Excel 2007, but connecting to an Excel 2003 formatted spreadsheet.

还有一点需要注意的是,我正在从 Excel 2007 建立连接,但连接到 Excel 2003 格式的电子表格。

TIA for any suggestions.

TIA 的任何建议。

回答by Nigel Heffernan

The answer is there in the connection string:

答案在连接字符串中:

HDR=YES;

HDR=是;

That clause instructs the database driver to read the first row of your file as a list of column or field names.

该子句指示数据库驱动程序将文件的第一行作为列名或字段名的列表读取。

Reccode it to:

将其重新编码为:

HDR=NO;

HDR=否;

There's additional documentation here: http://www.connectionstrings.com/excel

这里有其他文档:http: //www.connectionstrings.com/excel

The text driver page on ConnectionStrings.com has some interesting tips about IMEX=0 and other OLEDB apocrypha.

ConnectionStrings.com 上的文本驱动程序页面有一些关于 IMEX=0 和其他 OLEDB 伪经的有趣提示。