Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with uploading excel file

Posted on 2016-10-03
2
Medium Priority
?
89 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
[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
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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