Solved

Excel rows into DataSet

Posted on 2014-01-28
6
385 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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
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.

 

Author Comment

by:Jedidia
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

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

21 Experts available now in Live!

Get 1:1 Help Now