Caitlin Jojola

Caitlin Jojola

  • NA
  • 1
  • 761

Search entire excel workbook

Dec 19 2014 12:09 PM
I need help figuring out how to search my entire excel workbook. Below is the code to search one sheet from the workbook based on the text that the user enters. The results are displayed in a datagrid. Please help.  
 
private void btSearch_Click(object sender, RoutedEventArgs e)
{
OleDbConnection con;
OleDbCommand cmd;
OleDbDataAdapter dap;
DataSet dat;
string cstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\Users\\Owner\\Documents\\Visual Studio 2013\\Projects\\ATSDBTest\\SampleDB.xlsx\";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
string sql = String.Format("select * from [Sheet1$] where (Name like '%{0}%')", txsearch.Text);
con = new OleDbConnection(cstr);
cmd = new OleDbCommand(sql, con);
dap = new OleDbDataAdapter(cmd);
dat = new DataSet();
dap.Fill(dat, "searchresults");
dg.DataContext = dat.Tables["searchresults"];
}