Wednesday, January 23, 2019

How to read data from excel using OleDb in c# asp.net

In this article i have explain  read data from excel sheet using c# and fill data to gridview.

Add The gridview control to aspx page 

 <asp:GridView ID="GridView1" runat="server"></asp:GridView>

Add The Connection string into web.config file 

 <connectionStrings>
    <add name ="Excel03ConString"
         connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};
                         Extended Properties='Excel 8.0;HDR={1}'"/>
    <add name ="Excel07ConString"
         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
                         Extended Properties='Excel 8.0;HDR={1}'"/>
  </connectionStrings>






The following excel sheet read using c#



Now Add the following c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;

namespace demo
{
    public partial class readExcelUsingOleDb : System.Web.UI.Page
    {
        string sample = @"C:\brij\sample.xlsx";
        protected void Page_Load(object sender, EventArgs e)
        {
            getDataFromExcel();
        }
        protected void getDataFromExcel()
        {
            try
            {
                string conStr = "";
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                if (conStr != "")
                {
                    //Pass No for get Header
                    //Pass Yes For skip header
                    conStr = String.Format(conStr, sample, "Yes");

                    OleDbConnection connExcel = new OleDbConnection(conStr);
                    OleDbCommand cmdExcel = new OleDbCommand();
                    OleDbDataAdapter oda = new OleDbDataAdapter();
                    DataTable dt = new DataTable();
                    cmdExcel.Connection = connExcel;
                    //Get the name of First Sheet
                    connExcel.Open();
                    DataTable dtExcelSchema;
                    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    connExcel.Close();
                    //Read Data from First Sheet
                    connExcel.Open();
                    // Response.Write(" Sheet Name" + SheetName);
                    cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                    oda.SelectCommand = cmdExcel;
                    //fill data to datatable
                    oda.Fill(dt);
                    //fill datatable to gridview
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
        }
    }
}

Output:

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...