Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting individual cell info from Excel

Posted on 2006-07-03
7
Medium Priority
?
230 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

715 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