Select cell adress from Excel using OLEDB in C#

2019-08-30 06:43发布

问题:

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

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

回答1:

For the connection string I always go to ConnectionString.com. Search for the type of Excel file version your trying to open.

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



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

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'";