Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a excel spreadsheet with 3 tables of data in it. How do I read the data from this three different tables from excel spreadsheet and save to the database.
I have to implement this using c#.net
Posted
Comments
Herman<T>.Instance 20-May-15 5:06am    
odbc connection
CHill60 20-May-15 5:08am    
Anisuzzaman Sumon 20-May-15 5:15am    
Please explain "a excel spreadsheet with 3 tables of data" .That is how looks like your excel its format ,is those tables located in single worksheet or different!If you don't make rich your question It is quite impossible to answer.
Member 11560105 20-May-15 5:36am    
A single worksheet will have three different tables of data

1 solution

As digimanus said.. ODBC/OLEDB connection is the key.

Assuming, you have a WorkBook with three WorkSheets namely Sheet1, Sheet2 and Sheet3 with each sheet having data corresponding to one SQL table.

All you need to do is open an OLEDB connection on your workbook and read all the three sheets using this connection.

Assuming, you have a similar table structure in your database, you can drop this data into your database as it is.

Follow the steps below to achieve the above task:
1) Open OLEDB connection on Excel: To do this, you will mostly spend time to work out a perfect connection string which actually works on your Excel version(trust me it is painful sometimes!). check this link[^] to get some help with your connection string.
2) Read Data from Excel: Create an OLEDB Command object and fetch data from excel sheet just like the way you fetch data from SQL database and populate your data sets.
SQL
SELECT * FROM [Sheet1$]
SELECT * FROM [Sheet2$]
SELECT * FROM [Sheet3$]

3) Open a SQL connection: Using SqlConnection class with a specific connection string
4) Insert Data in SQL: Insert the data fetched in data sets from Excel in step 2 into SQL using SQL insert statement.
5) Close all connections and you are done!

You will find a lot of examples on internet. Also, if it is a one time activity, you can use SQL Server Import and Export Wizard[^].
 
Share this answer
 
v3

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