Click here to Skip to main content
15,072,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Let me know how to import data from excel to grid view.I want to click on button where a file dialog box opens,then i use to select excel sheet which is then imported to gridview.

How can i do it.

Thanks in advance...
Updated 3-May-11 20:21pm

There are 2 parts of your question

1- Uploading the excel file to the server
2- exporting the data to Gridview

1- use simple file uploader control and receives the file stream at server end. Remember your file is in memory it is not saved anywhere else.

2- Exporting operation requires source data and target control. In your case source data is in memory stream and target control is gridview. So you need to either save the uploaded file to a temp location on server and then use the path with the above examples to export it to Gridview, or you can use Excel interop assembly but you need to find a way to convert memory stream to Excel's WorkBook object.

Note: I'm not able to recall but excel oledb driver has some limitation in reading data because the driver analyze the data size to read based on first N number of rows. I think it has something like below:[^]

So i'll vote for Excel interop. I found a link which talks about exporting data to xml you can code similar for GridView.[^]

Prasad_Kulkarni 3-May-12 6:37am
Nice explanation Hemant,
Surely a +5!
Hemant__Sharma 3-May-12 13:17pm
Thank you Prasad :) I love +5 ;)
mylogics 4-May-11 2:26am
yaa its fine thank u bt i in this connection string is hardcoded.i want to get excel sheet anywhere from my computer.user select the excel sheet than it is imported.for ex as it happens in fileuploader.user select the file and then upload it.
Abhinav S 4-May-11 3:45am
it should be easy to open the excel file via the file open control.
Once the file is loaded, try to load sheets in that file into a dropdown and then let the user choose the sheet they want to import into the datagrid.

Hope this helps!

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