Query excel sheet in c#

The Select-command should look like this if you want to read A1 to D1:

SELECT * FROM [SHEETNAME_HERE$A1:D1]

Whole Code:

OleDbConnection con = new OleDbConnection(
    "provider=Microsoft.Jet.OLEDB.4.0;data source="
    + XLS_FILE_NAME_AND_PATH_HERE
    + ";Extended Properties=Excel 8.0;");

StringBuilder stbQuery = new StringBuilder();
stbQuery.Append("SELECT * FROM [" + SHEETNAME_HERE + "$A1:D1]");
OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);

DataSet dsXLS = new DataSet();
adp.Fill(dsXLS);

DataView dvEmp = new DataView(dsXLS.Tables[0]);

dataGridView1.DataSource = dvEmp;

Leave a Comment

tech