Solved

Help with uploading excel file

Posted on 2016-10-03
2
31 Views
Last Modified: 2016-10-10
Hi,

I'm using the code below to upload an excel file to  my application's folder, the code works but the columns with no data are not included in the xml file the excel file was converted to. How do I modify the code to include all the columns in the xml file?

 Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        OpenFileDialog1.InitialDirectory = "C:temp"
        OpenFileDialog1.Filter = "Excel File (*.xls)|*.xls"
        OpenFileDialog1.ShowDialog()
        TextBox2.Text = OpenFileDialog1.FileName.ToString()
        Button3.Enabled = True
    End Sub
    Dim conn As OleDbConnection
    Dim adap As OleDbDataAdapter
    Private Function makeDataTableFromSheetNameUpload() As DataSet
        Try
            Dim ds As DataSet = New DataSet()
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\Data.xls")
            conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            conn.Open()
            Dim dt As New DataTable()
            Dim sheetName As String = getSheetName(conn) 'Karrtik: Invoke the new function to get the first sheet name
            MsgBox(sheetName)
            adap = New System.Data.OleDb.OleDbDataAdapter("select * from [" & sheetName & "]", conn)
            adap.Fill(dt)
            ds.Tables.Add(dt)
            conn.Close()
            ds.Tables(0).WriteXml(Application.StartupPath + "\DataAOP40.xml")
            Return ds
        Catch
            MsgBox(Err.Description)
        End Try
    End Function
    Private Function getSheetName(objConn As OleDbConnection) As String
        Dim strSheetName As String = String.Empty
        Dim dtSheetNames As DataTable = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
        If dtSheetNames.Rows.Count > 0 Then
            Dim rowCollection As DataRowCollection = dtSheetNames.Rows
            strSheetName = rowCollection(0).Item("TABLE_NAME")
        End If
        Return strSheetName
    End Function
0
Comment
Question by:vcharles
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41827376
You need to use the "Empty Text Mode" provider properties. There are two possible values: NullAsEmpty or EmptyAsNull. I think NullAsEmpty should works better:
 conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;Empty Text Mode=NullAsEmpty"" ")

Open in new window

More information is here
0
 

Author Comment

by:vcharles
ID: 41827387
Hi,

Unfortunately both options don't work.

Private Function makeDataTableFromSheetNameUpload() As DataSet
        Try
            Dim ds As DataSet = New DataSet()
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\Data.xls")
            '  conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;Empty Text Mode=EmptyAsNull"" ")
            conn.Open()
            Dim dt As New DataTable()
            Dim sheetName As String = getSheetName(conn) 'Karrtik: Invoke the new function to get the first sheet name
            '   MsgBox(sheetName)
            adap = New System.Data.OleDb.OleDbDataAdapter("select * from [" & sheetName & "]", conn)
            adap.Fill(dt)
            ds.Tables.Add(dt)
            conn.Close()
            ds.Tables(0).WriteXml(Application.StartupPath + "\AOP40CTRY.xml")
            Return ds
        Catch
            MsgBox(Err.Description)
        End Try
    End Function
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

12 Experts available now in Live!

Get 1:1 Help Now