C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Getting Data From Excel to DataSet in Asp.Net C#


Posted Date: 08 Oct 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: PrashanthiMember Level: Silver    
Rating: 1 out of 5Points: 5



Getting Data from excel file to our .net program and filling it into the gridview



Take one FileUpload Control and one Button and GridView

In the codebehind page
write the code in Button Click event

protected void Button1_Click(object sender, EventArgs e)
{
StrName = FileUpload1.PostedFile.FileName;//here we are taking the excel file ex: emp.xls
DataTable myData = default(DataTable);
myData = GetDataFromExcel(StrName, "Sheet1$").Tables[0];
GridView1.DataSource = myData.DefaultView;
GridView1.DataBind();
}



public System.Data.DataSet GetDataFromExcel(string FileName, string RangeName)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
//here HDR means Header, if it "Yes" int incluedes Header if it is "No" it will come with out Headers

System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(strConn);
objConn.Open();

System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
System.Data.OleDb.OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter();
objDA.SelectCommand = objCmd;

// Fill DataSet
System.Data.DataSet objDS = new System.Data.DataSet();
objDA.Fill(objDS);

objCmd.Connection = objConn;
using (OleDbDataReader dr = objCmd.ExecuteReader())
{

string sqlConnectionString = "server=SeverName;Initial Catalog=Testing;uid=sa;pwd=**;";
// Bulk Copy to SQL Server
using (SqlBulkCopy bc = new SqlBulkCopy(sqlConnectionString))
{
bc.DestinationTableName = "emp";
bc.WriteToServer(dr);
}

}
objConn.Close();
return objDS;
}
catch
{
return null;
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Asp.net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sorting datatable - VBScript
Previous Resource: Copying Rows from One Datatable to Other
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use