简体   繁体   English

Excel SQL 查询如何指定列号而不是名称

[英]Excel SQL query how to specify a column number not name

I'm trying to query and filter an OleDataTable in C#, so not VBA inside of Excel.我正在尝试查询和过滤C#中的 OleDataTable,而不是 Excel 内部的 VBA。

string strSQL = "SELECT * FROM [Sheet1$] WHERE [Columns1$] is not null";  // to filter out any empty values, works if used column name like [UserInfo].
OleDbCommand cmd = new OleDbCommand(strSQL, connExcel);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(da);
dtData = new DataTable();
DataSet dtDataT = new DataSet();
dtData.Locale = System.Globalization.CultureInfo.InvariantCulture;
string FilePath = copyfile.ToString();
da.Fill(dtData);

in the place of [Columns1$] I've tried Columns(1) , Columns(A) , not suceeded.代替[Columns1$]我尝试了Columns(1)Columns(A) ,但没有成功。

You need to specify HDR=NO in your connection string, after which columns names will be assigned automatically as F1, F2, etc.您需要在连接字符串中指定 HDR=NO,之后列名将自动分配为 F1、F2 等。

https://www.connectionstrings.com/excel/ https://www.connectionstrings.com/excel/

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

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