Suresh Behera

The Microsoft .Net Junkies

News

Blogroll

Reading

Export Excel data to Datagrid

I was playing around excel and searching for some code which help me to export excel data to Datagrid.

Here is the code. 

Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Suresh\Test.xls;Extended Properties=Excel 8.0;"

Dim objConn As New System.Data.OleDb.OleDbConnection(strCon)

Dim daSuresh As New OleDbDataAdapter("Select * From [Suresh$]", strCon)

objConn.Open()

Dim dsSuresh As New DataSet

Dim dvSuresh As New DataView

daSuresh.Fill(dsSuresh, "Sheet1")

dvSuresh = New DataView(dsSuresh.Tables("Sheet1"))

dvSuresh.Sort = "name"

DataGrid1.DataSource = dsSuresh

DataGrid1.DataMember = dsSuresh.Tables("Sheet1").ToString

DataGrid1.DataBind()

objConn.Close()

Suresh Behera

Comments

TrackBack said:

# March 11, 2005 8:24 AM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 7:04 AM

kuldeep said:

i'm getting this error "External table is not in the expected format." when i tried to execute the code...

# November 17, 2006 2:54 AM

rakesh said:

can you help in exporting dtagrid to excel, so sipmle code.

# June 8, 2007 2:59 AM

Mekonnen said:

Hi Suresh

Thank you for your code that exports data from excel to Datagrid1. But I think the code donot work.

since it generates the following error

'Suresh$' is not a valid name.  Make sure that it does not include invalid characters or punctuation and that it is not too long.

can some one help me.

Thanks

# July 16, 2007 7:47 AM

Ashim said:

Please specify a more prominent code

# January 25, 2008 4:50 AM

anant said:

i need to export particular rows of datagrid, selected by checkbox..

can u have the solution??

regards

anant

# February 22, 2008 4:34 AM

Rajaram said:

Hi Suresh,

  I have written the code for importing the excel sheet values into the temp datagrid & stored those DG values into the database table, but my problem is if the person changed the sheet name from Sheet1 to somthing else means. If the sheet 1 is missing from the selected excel sheet it's throwing the error message like "Sheet1$' is not a valid name.Make sure that it does not include invalid characters or punctuation and that it is not too long.". can you please revert with your reply for this problem to my Inbox (Rajaram.i@annuk.com)

--

Warm Regards

I.Rajaram

Programmer Analyst

09915344754

ik.rajaram@gmail.com

# March 4, 2008 8:06 AM

Sathish,C said:

It Showing "Unspecified Error" in ASP.NET 2003

# May 8, 2008 10:47 AM

Naresh said:

how to export image..........

# August 18, 2008 4:33 AM

Raura said:

Mann! this is great!! thanks for helping me reduce the number of sooooo unnecessary code!

# November 6, 2008 9:21 AM

CB said:

This code is dependant on

Dim daSuresh As New OleDbDataAdapter("Select * From [Suresh$]", strCon)

Where [Suresh$] <--- Is the Title of the Spreadsheet Tab

The Default is [Sheet1$]

anything other than Default must be specified here to make the solution valid

# July 8, 2009 10:15 AM

Import Excel To Database « My Destination said:

Pingback from  Import Excel To Database &laquo;  My Destination

# July 21, 2010 6:23 AM

Ankur said:

Hi, Plz tell how did u create Object DataGrid1?

i know very lil about vb.

Thanks

# February 1, 2011 6:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)