Solved

Getting individual cell info from Excel

Posted on 2006-07-03
7
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

631 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