Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello sir,

I have excel sheet and it contain Name Field and ID field. In my database name as Personal which has Name and ID fields.Now I need to import data from that excel sheet and insert that all data into Personal table in my database.Please tel me how i do this.Data in excel sheet is given below as example
Plz help me.....

XML
Name    ID
a   1
b   2
Posted
Updated 2-Oct-12 10:31am
v2

You can copy and paste the excel data into a text box and on the "Save" button click, do as follows (remember that data in excel file gets copied as a delimited string, lines separated by "\r\n" or "\n" and columns by "\t"):

C#
//split out the rows first
string[] rows = textBox1.Text.Trim().Split(new char[]{'\r\n', '\n'}, StringSplitOptions.None);

//loop starts from 1 to skip the header row that contains captions i.e. Name, ID etc
for(int i=1;i<rows.length;i++)>
{
   //for each row we need to split out each individual column
   string[] columns = rows[i].Split(new char[]{'\t'}, StringSplitOptions.None);
    
   //this bit is pseudocode which you would replace with your SqlCommand etc
   //assign each column value to its corresponding table column
   tablecolumn[0].value = columns[0];
   tablecolumn[1].value = columns[1];
   tablecolumn[2].value = columns[2];
   ...
   //finally save this record
   SaveThisRow();
}


This is more or less the way I do my Excel imports, easy, simple and reasonably fast.
 
Share this answer
 
v2
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Oct-12 21:12pm    
Should be enough, a 5.
--SA
Maciej Los 3-Oct-12 16:05pm    
Thank you Sergey ;)
I have created a Excel AddIn framework that is very easy to customize to any export. You will still have to implement the export move the data from the class which this framework puts the data, and the database. Using Attributes in Excel Add-in Framework for Validating and Exporting Data[^]

Incidently if you are going to vote down, why say why you are voting down. Yes it does not provide a complete solution, but I would like to see you provide the first part of the solution that is better. How you connect to the database has many solutions, and is not dependent on being excel, it can be any database solution, and there are a lot of ways to solve that would apply to any application.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Oct-12 21:15pm    
Article? Reference? Otherwise, not helpful. :-)
I think, I found your article. It would probably make a great option, but you better need to reference it.
--SA
Clifford Nelson 2-Oct-12 21:25pm    
Thanks. Thought I had put it in the solution.
Sergey Alexandrovich Kryukov 2-Oct-12 23:13pm    
Well, my 5.
I also decided to up-vote your article and advised another member to up-vote.
--SA
Clifford Nelson 2-Oct-12 23:34pm    
Thanks for the vote of confidence
Sergey Alexandrovich Kryukov 3-Oct-12 13:48pm    
:-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900