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 » DataGridView »

Fetch data from Excel


Posted Date: 19 Aug 2009    Resource Type: Code Snippets    Category: DataGridView
Author: arunMember Level: Gold    
Rating: 1 out of 5Points: 5



The below code explains how you can fetch data from EXcel to a data adapter . Here I have used a OLEDB Connection

public void GetExcelData(string strFilename)
{
try
{
dataset dsExcelData;
dsExcelData.Clear();
string sCurrentSheet = "[Sheet1$]";
string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilename + ";Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection oConnection = new OleDbConnection(strConnectionString);
oConnection.Open();
OleDbCommand oCommand = new OleDbCommand("Select [MAKE BOX], [L],[W],[D],[ID],[SEQ],[PART],[PARTDESCRIPTION] from " + sCurrentSheet, oConnection);
OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(oCommand);
oDataAdapter.Fill(dsExcelData);// dataset
oConnection.Close();

}
catch (Exception)
{


}
}




// binding to a datagridview control


BindingSource bsDataBind = new BindingSource();
bsDataBind.DataSource = dsExcelData;
datagridview1.DataSource = bsDataBind;

or you can directly bind

datagridview1.datasourse= dsExcelData;




Responses

Author: Filip    20 Aug 2009Member Level: Silver   Points : 1
Nice code but OLEDB is slow. Try using GemBox spreadsheet component. It's much faster then OLEDB and easier to use. Here's are some examples how to read, write, export or convert spreadsheet files.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Fetch excel data using c#  .  Fetch data from Exce  .  Excel and dataa dapter  .  

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: Move GridView Rows with up and down keys
Previous Resource: Insertion of more than one record at a time.
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use