Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting individual cell info from Excel

Posted on 2006-07-03
7
Medium Priority
?
231 Views
Last Modified: 2008-02-01
Hi all,

Below is the code I have been using to extract all the data from an Excel Worksheet and put it into a Dataset :-

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & ImportFilePath & "; " & "Extended Properties=Excel 8.0;")
MyAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [" & SheetName & "$]", MyConnection)
MyConnection.Open()
MyDataset = New System.Data.DataSet
MyAdapter.Fill(MyDataset)
MyConnection.Close()

I am now trying to extract an specific cells info from Excel and put it into a specified cell on my datasheet. Can someone give me a code example of how I can acheive this.

Many Thanks
0
Comment
Question by:kiranboi
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:manch
ID: 17029950

If the Cell u needed is on rowno row and colno column then u can use

MyCellValue =  MyDataSet.Tables(0).Rows(rowno).Item(colno)

now u can use the MyCellValue as u want

0
 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 750 total points
ID: 17029976
Hi kiranboi,
----------

another way is selecting only a cell
http://support.microsoft.com/kb/316934/en-us

like Select * from [Sheet1$A1]

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:kiranboi
ID: 17030123
thats just what I needed bruintje but how can I insert the cell value into a specific point in a dataset? I'm using :-

 MyAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [" & SheetName & "$C3]", MyConnection)
                MyConnection.Open()
                MyAdapter.Fill(MyDataset.Tables(0).Rows(3).Item(0))
                MyConnection.Close()

but its telling me :- Additional information: No accessible overloaded 'OleDbDataAdapter.Fill' can be called without a narrowing conversion.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:bruintje
ID: 17030146
not sure but you can narrow down in the fill method to using 7 parameters including rowindex etc which will give you the option of inserting a single row in a dataset specified with a string parameter for the name

but i assume you cannot fill a specific cell exactly as you did with the column and item
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17030183
You cannot use a dataadapter to fill specific cells.  You will need to use a DataReader or, as you are interested in just one value, you can use ExecuteScalar.  On these lines

   Dim strSQL As String = <Your query returning one result>
   Dim cmd As New OleDbCommand(strSQL,<your connection>)
   cmd.Connection.Open
   <your cell> = cmd.ExecuteScalar
   cmd.Connection.Close

Roger
0
 

Author Comment

by:kiranboi
ID: 17030353
I'm using the code below and still getting problems.

I get an 'An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll' error on the line :-
MyDataset.Tables(0).Rows(0).Items(0) = cmd.ExecuteScalar

Any ideas what I'm doing wrong?

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & ImportFilePath & "; " & "Extended Properties=Excel 8.0;")
MyAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [" & SheetName & "$]", MyConnection)
Dim strSQL As String = MyAdapter.ToString
Dim cmd As New System.Data.OleDb.OleDbCommand(strSQL, MyConnection)
cmd.Connection.Open()
MyDataset.Tables(0).Rows(0).Items(0) = cmd.ExecuteScalar
cmd.Connection.Close()
0
 
LVL 34

Accepted Solution

by:
Sancler earned 750 total points
ID: 17030464
This is what you are doing wrong

MyAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [" & SheetName & "$]", MyConnection)
Dim strSQL As String = MyAdapter.ToString

What you want is just

Dim strSQL As String = "select * from [" & SheetName & "$]"

You are using something different from a DataAdapter, not a version of it.  A DataAdapter is really no more nor less than a pre-coded set of commands.  It lets you just say .Fill rather than having to code specific commands to get the table structure from the database, create the a datatable with the same structure in you application, call a DataReader to fill the datatable row by row.  (It also lets you ask it, once it has a Select command, to create the other commands that are needed to update the database and just call .Update.)  But in this setting, you just want a single command which you totally code yourself.

Roger
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question