vba 使用 SQL 查询没有标题行的 Excel 工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21511096/
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
Using SQL to query an Excel worksheet without a header row
提问by Bj?rn H. Sandvik
I have been searching for a solution for a while, and I find myself coming up empty handed.
我一直在寻找解决方案,但我发现自己两手空空。
The question is: Can you build a SQL query against a worksheet if you don't have a distinct header row for column references?
问题是:如果您没有用于列引用的不同标题行,您能否针对工作表构建 SQL 查询?
Easily enough:
很容易:
| A | B 1 | FirstName | LastName 2 | John | Davis 3 | Mary | Parker
SELECT [LastName] FROM [Sheet1$] WHERE [FirstName] = 'John'
--> "Davis"
--> "戴维斯"
However, I tend to work with header-less CSV files, so what could I do if I don't have the header row to steer by?
但是,我倾向于使用无标题的 CSV 文件,所以如果我没有标题行来引导我该怎么办?
| A | B 1 | John | Davis 2 | Mary | Parker
SELECT ??? FROM [Sheet1$] WHERE ??? = 'John'
To extend the question, it could equally interesting to know how to reference a row number - if possible - say I want to find the Last Name (column B) relative to row [2]
为了扩展这个问题,知道如何引用行号同样有趣 - 如果可能 - 说我想找到相对于行 [2] 的姓氏(B 列)
Thanks in advance for any input on my conundrum!
在此先感谢您对我的难题的任何意见!
-B
-B
回答by Meetu Singh
You can go through this link:
你可以通过这个链接:
http://www.sql-server-helper.com/tips/read-import-excel-file-p02.aspx
http://www.sql-server-helper.com/tips/read-import-excel-file-p02.aspx
Providing the HDR attribute as NO, will automatically name the columns as F1 to Fn.
提供 HDR 属性为 NO,将自动将列命名为 F1 到 Fn。