Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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.....
 
Name    ID
a   1
b   2
Posted 2-Oct-12 10:26am
n1985251
Edited 2-Oct-12 10:31am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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"):
 
//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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Sergey Alexandrovich Kryukov at 2-Oct-12 21:12pm
   
Should be enough, a 5.
--SA
Maciej Los at 3-Oct-12 16:05pm
   
Thank you Sergey ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 2-Oct-12 21:25pm
   
Thanks. Thought I had put it in the solution.
Sergey Alexandrovich Kryukov at 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 at 2-Oct-12 23:34pm
   
Thanks for the vote of confidence
Sergey Alexandrovich Kryukov at 3-Oct-12 13:48pm
   
:-)
--SA
Achha Insan at 3-Oct-12 5:35am
   
nothing is impossible. you can do it.
Sergey Alexandrovich Kryukov at 3-Oct-12 13:49pm
   
It depends: who is that "you" you've mentioned?
--SA
Achha Insan at 4-Oct-12 7:40am
   
all the best.
Maciej Los at 3-Oct-12 16:34pm
   
Clifford, please, let me understand...
OP wants to import data from MS Excel into SQL database. You're article is about validating data in MS Excel, using Interop... I'm little confused. How it could be helpful for OP? Am i missed something or am i blind?
BTW, very good articles.
Clifford Nelson at 3-Oct-12 17:02pm
   
The code can be used to help in creating a way to export data from excel. It also provides an easy way move data to an object, providing the user feedback on errors before actually committing to send the data. It still needs the code to actually do the export, but if you have field headers already defined, it can find those headers and read from there. All that needs to be done is to take the C# object and move it into the database.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try this
export to excel[^]
may be this will help you.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 338
2 OriginalGriff 175
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100