Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have an Excel sheet which contains records as follows, this is planning for 72 months, this means that the Excel sheet contains 74 columns including the first two columns.

PROD_CODE PLANT_CODE 201107 201108 201109 201110 201111 201112 201201
1222 INAA 34 44 45 44 34 454 4455
1425 INAC 67 788 787 78 887 888 6767


My SQL Server table contains 4 columns as follows:

1-PROD_CODE
2-PLANT_CODE
3-YEAR_MONTH
4-FORECAST



How can I read the Excel sheet in store into the SQL Server as follows:

PROD_CODE PLANT_CODE YEAR_MONTH FORECAST
1222 INAA 201107 34
1222 INAA 201108 44

Please someone help asap.
Posted
Updated 4-Aug-11 4:28am
v2
Comments
Herman<T>.Instance 4-Aug-11 10:10am    
if you did a search on codeproject.com you had found enough examples.
R. Giskard Reventlov 4-Aug-11 10:11am    
What have you tried for yourself?
LittleYellowBird 4-Aug-11 10:29am    
Hi, its best not to use capitals as it looks like shouting, so I've fixed this message for you. :)

1 solution

You can use the OpenRowset function to open the excel and select its content as if you were selecting from a table.
SQL
INSERT INTO YourTable (Col1, Col2, ........)
SELECT Col1, Col2, ......
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\YourExcel.xls', 'select * from [Sheet1$]')
 
Share this answer
 

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