.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Get excel datas into dataset.

Posted By:Dhivakaran       Posted Date: November 29, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

Here I'm going to display how do we get excel data into dataset object.


Create one class into your project and add below methods within your class and that method I have passed two parameter is "excelFileName"in this argument we need to pass the excel file name and another one is "SheetName".  in this parameter we have to pass sheetname of excel file.

and include below namespace within your class file

using System.Text;
using System.Data.OleDb;
using System.Data;

  public static DataSet GetExcelSheetData(String Filename, String SheetName)
            DataSet ds = new DataSet();
                conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Filename + ";Extended Properties=Excel 8.0;";
                string strsql = "select * from [" + SheetName + "]";
                OleDbConnection conn = new OleDbConnection(conStr);
                OleDbCommand cmd = new OleDbCommand(strsql, conn);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                return ds;
            catch (Exception ex)
                strError = ex.Message.ToString();
                return ds;
                if (!(ds.Equals(null)))
                    ds = null;

Steps 2:

Create one dataset object and assign the methods to dataset object  as like below code.

dataset ds=new dataset();


now we can able to see the datas in dataset object.


we have to put excel file in your project folder and get the files from that folder and process the same.

 Subscribe to Articles


Further Readings:

Author: vinay         Company URL: http://www.dotnetspark.com
Posted Date: December 13, 2010

its good. but could you please tell me how to open an password protected excel file(excel 2007) via asp.net page using cSharp.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend