Solved

Excel rows into DataSet

Posted on 2014-01-28
6
411 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

17 Experts available now in Live!

Get 1:1 Help Now