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.
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.
0 comments:
Please let me know your view