Solved

Getting individual cell info from Excel

Posted on 2006-07-03
7
223 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 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now