Solved

Excel rows into DataSet

Posted on 2014-01-28
6
433 Views
Last Modified: 2014-03-30
Below is an example of how my excel spread sheet is formatted.

example layout
I would like to take the contents and save them into a dataset.  The first eight columns are associated with each row to the right (if that makes sense).

I am able to query the rows that have data but that's as far as I have gotten.

Any guidance would be appreciated.

        Dim str As String = lsbTabs.GetItemText(lsbTabs.SelectedItem)

        Dim DtSet As System.Data.DataSet
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & someExcelFile & ";Extended Properties=""Excel 12.0;HDR=NO"";"

        MyConnection = New System.Data.OleDb.OleDbConnection(connstring)

        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" + str + "$] WHERE F1 <> '' AND F2 <> '' AND F3 <> '' AND F4 <> '' AND F5 <> ''", MyConnection)
0
Comment
Question by:Jedidia
[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
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39815497
"Fill" your dataset from your oledbdataadapter

MyCommand.Fill(DtSet)

I'm not exactly sure how the merged cells will affect the fill method. You might have to iterate through all the rows and update the fields in the merged columns with the values from the first row.
0
 

Author Comment

by:Jedidia
ID: 39815520
Thanks.  I fill the dataset and it only returns the first rows.   See the attached.  It skips all the rows with the "x"s.

example 2
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39815551
I'm guessing that has to do with the merged cells.
Which column is "F" that you have in your WHERE clause?
What do you get if you modify the WHERE criteria?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Jedidia
ID: 39815634
I remove the WHERE clause and the dataset looks like the below.

example 3
Perhaps I can manipulate the dataset to populate the missing data based on the first row..??
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 500 total points
ID: 39815723
Yes, you could probably come up with a way to loop through the rows to do that assuming the sort order remains intact.
Something like this should get you started. Basically, I'm checking for values in the first cells, if they are blank, use the values from the previous row.

dim dr as datarow
dim drPreviousRow as datarow
for x as integer = 0 to DtSet.tables(0).rows.count-1
dr=DtSet.tables(0).rows(x)
if dr.item(0) = "" AND dr.item(1) = "".... then
   if not drpreviousrow is nothing then
      dr.item(0) = drPreviousRow.item(0)
      dr.item(1) = drPreviousRow.item(1)....
   endif
endif
drPreviousRow = dr
next
0
 
LVL 10

Expert Comment

by:broro183
ID: 39961281
I can't tell if your cells are still meant to be merged, but it looks like they aren't merged in your last screenshot.

Perhaps I can manipulate the dataset to populate the missing data based on the first row..??

This vba code gives an example of how you can populate the missing data. It assumes that it is okay to convert the whole of the current region to values. If this isn't desired, the range can be changed using .Resize.


Option Explicit

Sub Macro1()
    With ActiveSheet.Range("A1").CurrentRegion
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
End Sub

Open in new window


hth
Rob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

749 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