Solved

Help with uploading excel file

Posted on 2016-10-03
2
72 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 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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