Monday 24 February 2014

Read Data from Excel Sheet in c#

Reading the data from excel sheet without opening it; there you need to just call the method into you application.

You need to connect to the excel data base by OLEDB connection and call the  excel sheet;

protected void btnShow_Click(object sender, EventArgs e)
        {
            GetData();
        }
      
        // save the data into dataview
        private void GetData()
        {
            DataTable sheetData = new DataTable();
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= 'c:\\Product DetailsEmployee.xlsx';Extended Properties='Excel 8.0;HDR=Yes;'";
            string query = "select * from [Sheet1$]";
            DataSet excelDataSet = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);
            da.Fill(excelDataSet);
            DgValue.DataSource = excelDataSet.Tables[0];
            DataView dv = ((DataTable)DgValue.DataSource).DefaultView;
            DataView dv_filter = new DataView();
            dv_filter.Table = excelDataSet.Tables[0];
            dv_filter.RowFilter = "Product_ID = '" + textBox1.Text + "'";
            DgValue.DataSource = dv_filter;
        }     

No comments:

Post a Comment