Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with uploading excel file

Posted on 2016-10-03
2
Medium Priority
?
98 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 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

926 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