Click here to Skip to main content
15,124,187 members
Articles / Web Development / ASP.NET
Posted 14 Dec 2012

Tagged as


39 bookmarked

Read Excel File into DataSet in ASP.NET Using C#

Rate me:
Please Sign up or sign in to vote.
4.74/5 (29 votes)
30 Jul 2013CPOL
How to read Excel file data into a DataSet in ASP.NET using C#.


In this example, I will explain how to read Excel file data into a DataSet in ASP.NET using C#. I have created an Excel file which contains the Slno, FirstName, LastName, and Location which is shown below:

We would be putting all the data into the grid view and with the help of filter with the dropdown list box, we would filter the Slno and display accordingly, else display all the cell values in the grid.


<asp:DropDownList ID="ddlSlno" runat="server" 
        AutoPostBack="true" AppendDataBoundItems="True">
<asp:ListItem Selected="True" 
Value="Select">- Select -</asp:ListItem>
<asp:GridView ID="grvData" runat="server">
<asp:Label ID="lblError" runat="server" />

C# Code

using System;
using System.Data.OleDb;
using System.Data;
using System.IO;
OleDbConnection oledbConn;
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
protected void ddlSlno_SelectedIndexChanged(object sender, EventArgs e)
private void GenerateExcelData(string SlnoAbbreviation)
            // need to pass relative path after deploying on server
            string path = System.IO.Path.GetFullPath(Server.MapPath("~/InformationNew.xlsx"));
            /* connection string  to work with excel file. HDR=Yes - indicates 
               that the first row contains columnnames, not data. HDR=No - indicates 
               the opposite. "IMEX=1;" tells the driver to always read "intermixed" 
               (numbers, dates, strings etc) data columns as text. 
            Note that this option might affect excel sheet write access negative. */

            if (Path.GetExtension(path) == ".xls")
                oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
                Data Source=" + path + ";
                Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
            else if (Path.GetExtension(path) == ".xlsx")
                oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
                Data Source=" + path + ";
                Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
            OleDbCommand cmd = new OleDbCommand(); ;
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            DataSet ds = new DataSet();

            // passing list to drop-down list

            // selecting distinct list of Slno 
            cmd.Connection = oledbConn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT distinct([Slno]) FROM [Sheet1$]";
            oleda = new OleDbDataAdapter(cmd);
            oleda.Fill(ds, "dsSlno");
            ddlSlno.DataSource = ds.Tables["dsSlno"].DefaultView;
            if (!IsPostBack)
                ddlSlno.DataTextField = "Slno";
                ddlSlno.DataValueField = "Slno";
            // by default we will show form data for all states 
            // but if any state is selected then show data accordingly
            if (!String.IsNullOrEmpty(SlnoAbbreviation) && SlnoAbbreviation != "Select")
                cmd.CommandText = "SELECT [Slno], [FirstName], [LastName], [Location]" +
                    "  FROM [Sheet1$] where [Slno]= @Slno_Abbreviation";
                cmd.Parameters.AddWithValue("@Slno_Abbreviation", SlnoAbbreviation);
                cmd.CommandText = "SELECT [Slno],
                [FirstName],[LastName],[Location] FROM [Sheet1$]";
            oleda = new OleDbDataAdapter(cmd);

            // binding form data with grid view
            grvData.DataSource = ds.Tables[1].DefaultView;
        // need to catch possible exceptions
        catch (Exception ex)
            lblError.Text = ex.ToString();
}// close of method GemerateExceLData


When you select the value from the dropdown list, the result would be: 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mannava Siva Aditya
Web Developer
India India
I am a 29 year old software Web Developer from Hyderabad, India. I have been working since approximately age 25. Where as in IT Development industry since 27. I am Microsoft Certified Technology Specialist.

I have taught myself in development, beginning with Microsoft's technologies ASP.NET, Approximately 3 years ago, I was given an opportunity to work as a freelance in the tech field. Now I am working as a web developer where my roles make me purely in web based technology solutions which manage and control access to applications and patient information stored in legacy systems, client-server applications.

I too had an opportunity to train some IT professionals with technical skills in development area. Which became my passion.

I have worked on various .NET framework versions(2.0 , 3.5, 4.0) and have been learning every new technology being introduced. Currently, I am looking forward to working in R & D in .Net to create distributed, reusable applications.

Comments and Discussions

QuestionAvoid hard coding Sheetname Pin
Sangram Nandkhile28-Aug-15 5:54
MemberSangram Nandkhile28-Aug-15 5:54 
AnswerRe: Avoid hard coding Sheetname Pin
Mannava Siva Aditya13-Oct-15 3:37
MemberMannava Siva Aditya13-Oct-15 3:37 
Questionsome Rows getting Blank after importing to dataset Pin
Sachin MCA201211-Aug-15 7:22
MemberSachin MCA201211-Aug-15 7:22 
QuestionHow can I query with Slno for entire Row Data Pin
Member 1162173113-Jun-15 1:26
MemberMember 1162173113-Jun-15 1:26 
QuestionHow to avoid hidden columns while reading excel Pin
karthikv10127-May-15 20:01
Memberkarthikv10127-May-15 20:01 
AnswerRe: How to avoid hidden columns while reading excel Pin
Mannava Siva Aditya26-Jul-15 21:00
MemberMannava Siva Aditya26-Jul-15 21:00 
GeneralMy vote of 4 Pin
Dinesh_Shaw9-Nov-14 0:00
MemberDinesh_Shaw9-Nov-14 0:00 
QuestionAn error occured while running it Pin
Santhosh Sabarissh KM24-Aug-14 8:26
MemberSanthosh Sabarissh KM24-Aug-14 8:26 
AnswerRe: An error occured while running it Pin
Mannava Siva Aditya16-Nov-14 20:50
MemberMannava Siva Aditya16-Nov-14 20:50 
GeneralNicely done and informative. Thanks. Pin
Serge-USA28-Jul-14 9:27
MemberSerge-USA28-Jul-14 9:27 
GeneralMy vote of 4 Pin
Anwar Hossin28-Aug-13 1:33
MemberAnwar Hossin28-Aug-13 1:33 
GeneralRe: My vote of 4 Pin
Mannava Siva Aditya28-Aug-13 19:06
MemberMannava Siva Aditya28-Aug-13 19:06 
GeneralMy vote of 5 Pin
raj ch1-Aug-13 1:17
Memberraj ch1-Aug-13 1:17 
GeneralRe: My vote of 5 Pin
Mannava Siva Aditya28-Aug-13 19:06
MemberMannava Siva Aditya28-Aug-13 19:06 
GeneralMy vote of 1 Pin
philipli31-Jul-13 4:24
Memberphilipli31-Jul-13 4:24 
GeneralRe: My vote of 1 Pin
Mannava Siva Aditya31-Jul-13 4:32
MemberMannava Siva Aditya31-Jul-13 4:32 
GeneralMy vote of 5 Pin
FernandoUY30-Jul-13 4:56
professionalFernandoUY30-Jul-13 4:56 
GeneralMy vote of 5 Pin
Leo Jian24-Jul-13 16:03
MemberLeo Jian24-Jul-13 16:03 
GeneralRe: My vote of 5 Pin
Mannava Siva Aditya24-Jul-13 20:28
MemberMannava Siva Aditya24-Jul-13 20:28 
GeneralMy vote of 1 Pin
Farhan usman6-Jun-13 21:06
MemberFarhan usman6-Jun-13 21:06 
GeneralRe: My vote of 1 Pin
Mannava Siva Aditya16-Jul-13 0:55
MemberMannava Siva Aditya16-Jul-13 0:55 
GeneralRe: My vote of 1 Pin
Mannava Siva Aditya24-Jul-13 2:28
MemberMannava Siva Aditya24-Jul-13 2:28 
GeneralMy vote of 3 Pin
Singh_Vinay29-May-13 22:39
professionalSingh_Vinay29-May-13 22:39 
GeneralRe: My vote of 3 Pin
Mannava Siva Aditya30-May-13 0:50
MemberMannava Siva Aditya30-May-13 0:50 
GeneralThanks Pin
Alireza_136212-May-13 5:54
MemberAlireza_136212-May-13 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.