简体   繁体   中英

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.

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.

You need to specify HDR=NO in your connection string, after which columns names will be assigned automatically as F1, F2, etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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