Thursday, 31 August 2017

How to Read (Import) MS Excel File And Populate Into GridView Using Asp.Net in C#

8/31/2017 - By Pranav Singh 0

In this article I will show you how you can read (import) ms excel file and populate into gridview using asp.net in c#. So this article will cover how to import data from excel to gridview in c#, how to read excel file in asp.net using file upload control, how to import data from gridview to excel in asp.net using c#, how to read data from excel file in asp.net using c#, import data from excel to datagridview in asp net, display excel sheet in asp.net web page, how to import data from excel to gridview in c# web application, excel datasource c#, Display Excel file data into Gridview in asp.net.

Some of my previous articles are as follows: Export GridView Data To Excel Sheet Using C#.Net In Windows Application, Windows Application - Excel Sheet Name in C#.Net, Get Or Retrieve Excel File Sheet Name Using ADO.Net in Asp.net Using C#, Excel Sheet Import and Read Data using ADO.Net in Asp.net Using C#, Import & Save Excel Sheet Data Into SQL Server Database Table In Asp.net, Excel File Upload Or Import and Display In GridView Using C# In Asp.Net.

So for this article first we will create an excel sheet and add some value in it.



Now we will add the excel sheet into the folder in the application.


Now we will create a new asp.net application and add the below grid view code into the page.

<form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" EnableViewState="false" />
        </div>
</form>

After this we write the code to read and bind the excel sheet data to the gridview control.

string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
            string filepath = Server.MapPath("~/ExcelFileList/StudentExcelSheet.xlsx");
            DataTable dt = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(string.Format(connectionstring, filepath)))
            {
                string query = @"SELECT * FROM [Sheet1$]";
                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    conn.Open();
                    using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                    conn.Close();
                };
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();

Here in above code I have first taken the connectionstring for which contains the oledb provider for reading the excel sheet. After that I have taken the taken the path of the excel sheet and then read the excel sheet data in into datatable and bind it to grid view.

Now we have done run the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top