Click here to Skip to main content
15,886,565 members
Articles / Web Development / ASP.NET
Tip/Trick

Loading Excel data into a GridView

Rate me:
Please Sign up or sign in to vote.
1.70/5 (6 votes)
2 Jan 2010CPOL 26.1K   4   2
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;protected void Page_Load(object...
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;

protected void Page_Load(object sender, EventArgs e)
{
	GetExcelSheetNames("Path");
}

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


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


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)



Comments and Discussions

 
GeneralReason for my vote of 1 replica Pin
raj ch20-Oct-11 23:03
raj ch20-Oct-11 23:03 
Reason for my vote of 1
replica
GeneralReason for my vote of 1 there is anothr alternative whick is... Pin
genious Developer6-Oct-11 19:26
genious Developer6-Oct-11 19:26 

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.