473,416 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

import different excel sheet to ONE sql table

Hello,

I have a lot of excel sheets which columns are slightly different, i want to import all of these sheets (one at a time) into ONE SQL TABLE. I'll give an example :

Say ive written the required program and called it Excel2sql converter. So Excel2sql takes an excel sheet and create a database table with the data of that excel sheet, For example say i have the following excel sheet:

Excel_Sheet_1
-----------------------------
FirstName MiddleName LastName
John------- A. -------- Smith


when i run Excel2sql (Excel_Sheet_1), a database table should be CREATED for me with the following data in it:


FirstName MiddleName LastName
John------- A. -------- Smith



Now, when i run my program again with the following excel sheet:

Excel_Sheet_2
-----------------------------
LastName FirstName MiddleName
Kerry------- M. --------- wolf


i should get the following UPDATED db table:

FirstName MiddleName LastName
John-------- A. --------- Smith
Kerry------- M. --------- wolf


Notice that it added the data of excel sheet 2, into the already existing database table.
It did some kind of mapping between the columns of db and the columns of the excel sheet 2 to append the data appropriately.


Now, if i run my program again with the following excel sheet:

Sheet 3
--------
PhoneNumber LastName MiddleName FirstName
232-232 ----- Lame -------- K. ------ Phoebe



i should get the following db table:

FirstName MiddleName LastName PhoneNumber
John-------- A. --------- Smith--------null
Kerry------- M. --------- wolf --------null
Phoebe------ K. --------- Lame --------232-232

I want the code to do this dynamically, i mean, anybody can use my code, give it an excel sheet as an input, my code will then CREATE for him an sql data table and each time a user gives him an excel sheet, it should UPDATED the already created sql table.

Please i really need all the help i can get. Im very new at this. I wrote a primitive code that simply uploads one excel sheet into an ALREADY existing datatable. (which is not what i want , but i had to start somewhere)

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. using System.Data.SqlClient;
  9. using System.Linq;
  10. using System.Web;
  11. using System.Web.Security;
  12. using System.Web.UI;
  13. using System.Web.UI.HtmlControls;
  14. using System.Web.UI.WebControls;
  15. using System.Web.UI.WebControls.WebParts;
  16. using System.Xml.Linq;
  17. public partial class _Default : System.Web.UI.Page 
  18. {
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.  
  22.     }
  23.  
  24.     protected void insertdata_Click(object sender, EventArgs e)
  25.     {
  26.         //-----------------connection to excel=--------------------------
  27.         OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("example.xls") + ";Extended Properties=Excel 8.0");
  28.         try
  29.         {
  30.             //-----------------Commad to get all columns---------------------
  31.             OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
  32.  
  33.             //-----------------open  the connection-----------------------
  34.             oconn.Open();
  35.  
  36.               //-----------------execute the command ----------------------
  37.             OleDbDataReader odr = ocmd.ExecuteReader();
  38.             string fname = "";
  39.             string lname = "";
  40.             string mobnum = "";
  41.  
  42.               //-----------------read from the datareader-------------------
  43.             while (odr.Read())
  44.             {
  45.  
  46.                    //-----------------insert into the db table ------------------ 
  47.                 insertdataintosql(fname, lname, mobnum);
  48.             }
  49.             oconn.Close();
  50.         }
  51.         catch (DataException ee)
  52.         {
  53.             lblmsg.Text = ee.Message;
  54.           }
  55.         finally
  56.         {
  57.             lblmsg.Text = "Data Inserted Sucessfully";
  58.         }
  59.     }
  60.  
  61.     public void insertdataintosql(string fname, string lname, string mobnum)
  62.     {
  63.         //-----------------connection to sql database----------------
  64.  
  65.         SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
  66.  
  67.  
  68.         SqlCommand cmd = new SqlCommand();
  69.         cmd.Connection = conn;
  70.         cmd.CommandText = "insert into dbtable(fname,lname,mobnum) values(@fname,@lname,@mobnum)";
  71.         cmd.Parameters.Add("@fname", SqlDbType.NVarChar).Value = fname;
  72.         cmd.Parameters.Add("@lname", SqlDbType.NVarChar).Value = lname;
  73.         cmd.Parameters.Add("@mobnum", SqlDbType.NVarChar).Value = mobnum;
  74.  
  75.         cmd.CommandType = CommandType.Text;
  76.         conn.Open();
  77.         cmd.ExecuteNonQuery();
  78.         conn.Close();
  79.     }
  80.  
  81. }
  82.  
  83.  
Again the code ive written is NOT what i want, can you please help me modify it to get the requirements i want!

Thank you in advance :)
Oct 19 '10 #1
1 1944
ck9663
2,878 Expert 2GB
I'm not familiar with the code you posted. Looks like a C/Java-based language. Search your language if there's a way you can retrieve the number of sheets in a workbook. Then loop through that...

Good Luck!!!

~~ CK
Oct 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
3
by: Danny | last post by:
Hi again I am trying to import from an excel spreadsheet into access 2002, but the order that is in the spreadsheet is not preserved when I import. using DoCmd.TransferSpreadsheet in code. ...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
0
by: bimalkumar | last post by:
hi, can anybody tell me a very simple code to import a Excel sheet data to flux grid in visual basic thanks
3
by: DONE1 | last post by:
Hello, I am trying to import 4 separate csv files from 4 different servers into an excel sheet.I want to write a macro which will automate this.Also, i want to append the name of the server for each...
11
by: MD | last post by:
Hello, I need to import a sheet of 884 different excel-file with same lay- out. The sheet name is 'Totaal' and is the same in all different files. Is there a script (module) in order to: 1....
1
by: schatzy | last post by:
HI ALL, I am having multiple .xls files in a single directory. I want to select some specific files and import them in a Master file by a single click of a button (add the sheet in my active...
4
by: laurasesma18 | last post by:
I've got a convertion tool with Access 2003. It imports an excel sheet into the data base and with the records in it creats different outputfiles. Its programmed with VBA. Now what I need is, in...
3
by: inepu | last post by:
I have some excel sheets that I would like to manage in access. I've created the access tables and relationships; the excel sheet is only one table and with duplicate data, I'd like to make sure that...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.