How to read data of an Excel file using C#?
By using this code you can read Excel sheet data into dataset. First you have to create a project then create file upload control and a submit button and on button click write the following code.
Code:
Please share this post, if it's useful for you. Thanks!
By using this code you can read Excel sheet data into dataset. First you have to create a project then create file upload control and a submit button and on button click write the following code.
Code:
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.OleDb; public partial class _ParseExcel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnParseExcel_Click(object sender, EventArgs e) { string path = System.IO.Path.GetFullPath(uploadFile.PostedFile.FileName); string connString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data source="+path +";" + "Extended Properties=Excel 8.0;"; OleDbConnection oledbConn = new OleDbConnection(connString); try { oledbConn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn); OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; DataSet ds = new DataSet(); oleda.Fill(ds, "Table"); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } catch { } finally { oledbConn.Close(); } } }
Please share this post, if it's useful for you. Thanks!
3 comments:
hi ,
great post
i write post on same concept
http://csharpektroncmssql.blogspot.in/2012/04/how-to-read-data-from-excel-file-in.html
hi,
good post.But i am getting an error at this line :
"OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);"
that ---> "The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly."
could u please help me with this.
You can also read data from excel file by using any .NET Library like Aspose has for excel and it offers many more functions that users can embed in their applications.
Post a Comment
Share your thoughts here...