?
Solved

Help with loading data from excel file by workbook's name

Posted on 2014-11-05
3
Medium Priority
?
219 Views
Last Modified: 2014-11-06
Hi,

How do I modify the code below to load my DataGrids with multiple workbooks from an excel file by using the name of the workbook, when attempting to use the current approach the workbook does not match ds.Tables(x). For example the 5th workbook does not match ds.Tables(5)

 Try
          
            ' InitializeComponent()
            'MsgBox(username.Text)
            '  Dim filename As String = "\Data\MasterFiles\combineSheets.xls"
            Dim filename As String = Application.StartupPath & "\Data\MasterFilesExcel\MasterFiles" & username.Text & ".xls"

            'Dim filename As String = Application.StartupPath & "\Data\LinkFiles\aop29combinedaa.xls"
            Dim myConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & filename & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            myConnection.Open()
            Dim mySheets As DataTable = myConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
            ' Dim ds As New DataSet()
            'Dim dt As DataTable

            For i As Integer = 0 To mySheets.Rows.Count - 1
                dt = makeDataTableFromSheetName(filename, mySheets.Rows(i)("TABLE_NAME").ToString())
                ds.Tables.Add(dt)
            Next
            Master.Visible = True
            Master.C1TrueDBGrid1.DataSource = ds.Tables(1)
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(1).AutoSize()
            Master.C1TrueDBGrid1.Splits(0).DisplayColumns(2).Visible = False
            '****************************************************************************
            Master.C1TrueDBGrid2.DataSource = ds.Tables(13)
            Master.C1TrueDBGrid2.Splits(0).HighLightRowStyle.BackColor = Drawing.Color.Blue
            Master.C1TrueDBGrid2.Splits(0).HighLightRowStyle.ForeColor = Drawing.Color.White

            'For Each row As DataRow In dtsetcol13.Tables(0).Rows
            'Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Values.Add(New C1.Win.C1TrueDBGrid.ValueItem(row("C_ID"), row("C_ID")))
            'Next

            ' Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Validate = True
            'Master.C1TrueDBGrid2.Columns("C_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.SortedComboBox
            ' Master.C1TrueDBGrid2.Columns(0).Caption = "W_ID"
            'Master.C1TrueDBGrid2.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid2.Splits(0).DisplayColumns(0).AutoSize()
            Master.C1TrueDBGrid2.Splits(0).DisplayColumns(1).AutoSize()
             Master.C1TrueDBGrid2.Splits(0).DisplayColumns(2).Visible = False
                     '****************************************************************************
            'Exit Sub
            Master.C1TrueDBGrid3.DataSource = ds.Tables(10)

            Master.C1TrueDBGrid4.DataSource = ds.Tables(12)
            Master.C1TrueDBGrid5.DataSource = Nothing

            Master.C1TrueDBGrid7.DataSource = ds.Tables(11)
            Master.C1TrueDBGrid8.DataSource = ds.Tables(6)
            Master.C1TrueDBGrid9.DataSource = ds.Tables(9)
            Master.C1TrueDBGrid10.DataSource = Nothing
            Master.C1TrueDBGrid11.DataSource = Nothing
            Master.C1TrueDBGrid6.DataSource = ds.Tables(0)

            Master.C1TrueDBGrid13.DataSource = ds.Tables(10)
            Master.C1TrueDBGrid14.DataSource = ds.Tables(6)
            Master.C1TrueDBGrid15.DataSource = ds.Tables(4)
            Master.C1TrueDBGrid16.DataSource = ds.Tables(5)
            Master.C1TrueDBGrid17.DataSource = ds.Tables(9)

            Master.C1TrueDBGrid18.DataSource = ds.Tables(13)
            Master.C1TrueDBGrid19.DataSource = ds.Tables(14)
            Master.C1TrueDBGrid20.DataSource = Nothing
            Master.C1TrueDBGrid21.DataSource = Nothing

           
        Catch

            MsgBox(Err.Description)
        End Try

Open in new window

0
Comment
Question by:vcharles
[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 Comments
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 2000 total points
ID: 40426004
dont know what the makeDataTableFromSheetName does exactly , but may be good idea to set name of table as i dont believe you can be certain of the order.

and in any case , ds.Tables(5) would be the sixth , as it starts from zero.

            For i As Integer = 0 To mySheets.Rows.Count - 1
                dt = makeDataTableFromSheetName(filename, mySheets.Rows(i)("TABLE_NAME").ToString()).
                dt.Name = filename    '<<< check for valid characters ??
                ds.Tables.Add(dt)
            Next

now you can use ds.Tables("myName1") instead of ds.Tables(11)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40426238
"For example the 5th workbook does not match ds.Tables(5)"
Since the collection is 0-based, the 5th worksheet would be ds.Tables(4).
0
 

Author Closing Comment

by:vcharles
ID: 40426257
Thank You!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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