在C#中使用OLEDB从Excel选择单元格地址 [英] Select cell adress from Excel using OLEDB in C#
问题描述
我有以下连接字符串:
string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=template.xls;Extended Properties=""Excel 12.0 XML;""";
连接成功.而且我在Excel工作表中有以下数据
Connection is successful. And i have following data in excel sheet
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
推荐答案
对于连接字符串,我总是转到 ConnectionString.com . 搜索您要打开的Excel文件版本的类型.
For the connection string I always go to ConnectionString.com. Search for the type of Excel file version your trying to open.
我猜应该是:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=template.xls;Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
具有SQL查询字符串,对于excel,您应该这样做
Has for the SQL query string, for excel you should do it like this
"SELECT * FROM [<SheetName>$<optional range>]";
例如,如果工作表名称为MySheet,则可以
For exemple if the worksheet name is MySheet you can do
"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'";
这篇关于在C#中使用OLEDB从Excel选择单元格地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!