ADO-如何从xls文件中选择两个或多个具有相同名称的列? [英] ADO - how to select column from xls file where two or more columns have the same name?

查看:96
本文介绍了ADO-如何从xls文件中选择两个或多个具有相同名称的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的excel文件:

I have an excel file like this:

|   |    A   |    B    |    C    |   D    |
| 1 | Name 1 |  Name 2 |  Name 3 | Name 2 |
| 2 |  Data  |  Data   |  Data   | Data   |
| 3 |  Data  |  Data   |  Data   | Data   |

如您所见,两列的标题具有相同的名称-名称2

As you can see, headers of two columns have the same name - Name 2.

我的问题是,是否可以告诉ADO引擎从哪一列中选择数据?

My question is, is it possible to tell the ADO engine from which column to select data?

当前,我的选择如下所示:

Currently my select looks like this:

SELECT [Name 1], [Name 2] FROM [REPORT7_RAW$] WHERE [Name 1] IS NOT NULL

并且ADO从列<$中列出的列中提取数据c $ c> B 在excel中。换句话说,它采用具有给定名称的第一列。不幸的是,我有两个名称相同的列,我想从列 D 中提取数据。可能吗?

and ADO picks up the data from column which is listed under column B in excel. In other words it takes the first column which have the given name. Unfortunately I have two columns with the same name and I would like to pull out the data from column D. Is it possible?

我找不到通过索引来选择列而不是名称的方法。

I could not find any way to select column by its index rather the name.

推荐答案

您将需要更改连接字符串,以便不使用数据头名称。普通的连接字符串如下所示:

You will need to change your connection string so that data header names are not used. The normal connection string would look something like this:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";

您需要更改最后一位, HDR = YES HDR = NO

You need to change the last bit, HDR=YES, to HDR=NO.

在这种连接方式下,列(字段)变为 F1 F2 等,其中 F1 =列A, F2 =列B,等等。

With that type of connection, the columns(fields) then become F1, F2, etc., where F1 = column A, F2 = column B, etc.

这并不理想,因为您现在实际上正在运行查询

This is not ideal, since you are now essentially running the query based on the number of the column rather than the name, but with duplicate column names, this is the only way around that.

根据@barrowc的注释:这种格式是基于列号而不是名称,但是具有重复的列名。连接字符串的会将您的列名视为数据。因此,根据您的查询,您可能需要包含代码以过滤出包含列名的行。

Per the comment from @barrowc: This format of the connection string will treat your column names as data. So depending on your query, you may need to include code to filter out the row that contains your column names.

这篇关于ADO-如何从xls文件中选择两个或多个具有相同名称的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆