简体   繁体   English

在C#中使用OLEDB从Excel选择单元格地址

[英]Select cell adress from Excel using OLEDB in C#

I have a following connection string: 我有以下连接字符串:

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=template.xls;Extended Properties=""Excel 12.0 XML;""";

Connection is successful. 连接成功。 And i have following data in excel sheet 我在Excel工作表中有以下数据

ID  Channel Upload
2_b_20_1    1   0,0 Mbps
2_b_20_2    2   0,0 Mbps
2_b_20_3    3   0,0 Mbps
2_b_20_4    4   0,0 Mbps
2_b_20_5    5   0,0 Mbps
2_b_20_6    6   0,0 Mbps
2_b_20_7    7   0,0 Mbps
2_b_20_8    8   0,0 Mbps
2_b_20_9    9   0,0 Mbps
2_b_20_10   10  0,0 Mbps
2_b_20_11   11  0,0 Mbps
2_b_20_12   12  0,0 Mbps
2_b_20_13   13  0,0 Mbps

I need to find address of cell containing string in the first column. 我需要在第一列中找到包含字符串的单元格的地址。 So in pseudo select it would be like: 所以在伪选择中,它将像:

Select "CellAdress" from [MySheet] where Value like '2_b_20_1'

and it should return address of this cell. 它应该返回该单元格的地址。

I dont have any code yet about it, i just dont know where to start from. 我还没有任何代码,我只是不知道从哪里开始。

Is it possible at all? 有可能吗? Thank you in advance 先感谢您

For the connection string I always go to ConnectionString.com . 对于连接字符串,我总是转到ConnectionString.com Search for the type of Excel file version your trying to open. 搜索您要打开的Excel文件版本的类型。

I'm guessing for you it should be: 我猜应该是:



    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=template.xls;Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";

Has for the SQL query string, for excel you should do it like this 对于SQL查询字符串,对于excel,您应该这样做



    "SELECT * FROM [<SheetName>$&ltoptional range>]";

For exemple if the worksheet name is MySheet you can do 例如,如果工作表名称为MySheet,则可以执行



    "SELECT * FROM [MySheet$]";

If you want to select a specific range you can do it like this. 如果您想选择一个特定范围,您可以这样做。



    "SELECT * FROM [MySheet$A1:C200]";

In your case (without knowing the rest of your code is hard) but I believe what you need is: 在您的情况下(不知道其余代码很难),但是我相信您需要的是:



    "SELECT * FROM [MySheet$] WHERE ID='2_b_20_1'";

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

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