Click here to Skip to main content
15,867,308 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Article

Loading Excel data into a GridView

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL 6.3K   1  
Hello All,Here I am posting code, which will read through a Excel Document. This code will traverse through all sheets of Excel spread sheet, No

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Hello All,

Here I am posting code, which will read through a Excel Document. This code will traverse through all sheets of Excel spread sheet, No matter what name they will have.

This uses OLEDB connection to read through excel sheets.

using System.Data.OleDb;<br /><br />protected void Page_Load(object sender, EventArgs e)<br />{<br />	GetExcelSheetNames("Path");<br />}<br /><br />private void GetExcelSheetNames(string excelFile)<br />{<br />	OleDbConnection objConn = null;<br />	System.Data.DataTable dt = null;<br />	try<br />	{<br />		DataSet ds = new DataSet();<br />		// Connection String. <br />		String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";<br />		// Create connection. <br />		objConn = new OleDbConnection(connString);<br />		// Opens connection with the database. <br />		objConn.Open();<br />		// Get the data table containing the schema guid, and also sheet names. <br />		dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);<br />		if (dt == null)<br />		{<br />			return;<br />		}<br />		String[] excelSheets = new String[dt.Rows.Count];<br />		int i = 0;<br />		// Add the sheet name to the string array. <br />		// And respective data will be put into dataset table <br />		foreach (DataRow row in dt.Rows)<br />		{<br />			excelSheets[i] = row["TABLE_NAME"].ToString();<br />			OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);<br />			OleDbDataAdapter oleda = new OleDbDataAdapter();<br />			oleda.SelectCommand = cmd;<br />			oleda.Fill(ds, "TABLE");<br />			i++;<br />		}<br />		// Bind the data to the GridView <br />		GridView1.DataSource = ds.Tables[0].DefaultView;<br />		GridView1.DataBind();<br />		Session["Table"] = ds.Tables[0];<br />	}<br />	catch (Exception ex)<br />	{<br />		Response.Write(ex.Message);<br />		<br />	}<br />	finally<br />	{<br />		// Clean up. <br />		if (objConn != null)<br />		{<br />			objConn.Close();<br />			objConn.Dispose();<br />		}<br />		if (dt != null)<br />		{<br />			dt.Dispose();<br />		}<br />	}<br />}<br />protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)<br />{<br />	GridView1.PageIndex = e.NewPageIndex;<br />	GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;<br />	GridView1.DataBind();<br />}<br />

 

In .aspx file

<asp:GridView ID="GridView1" <br />              runat="server" <br />              AllowPaging="true" <br />              PagerSettings-Mode="Numeric" <br />              PagerSettings-Position="Bottom" <br />              PagerStyle-Font-Size="Medium" <br />              PageSize = "10" <br />              OnPageIndexChanging="GridView1_PageIndexChanging" ><br /></asp:GridView><br />

 

This will have the data from the excel document in a gridview, with paging.

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --