Results 1 to 6 of 6

Thread: Opening XLS file through OleDb. Header issues.

Threaded View

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Angry Opening XLS file through OleDb. Header issues.

    Hello, there is a problem I'm afraid there is no solution for, but I wish to ask anyway just in case someone knows.

    My application downloads a third-party XLS file from the web. I cannot change its format so I have to teach my application to open it as it is.

    I'm using OleDb for opening, here's my code:

    vb Code:
    1. '
    2. If XLSFile = "" Then Exit Sub
    3. Dim sExcelConn As String =  _
    4.     String.Format( _
    5.      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";", XLSFile)
    6.  
    7. Dim conn As New OleDbConnection(sExcelConn)
    8. Dim ds As New DataSet()
    9. conn.Open()
    10.  
    11. Dim dtschema As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    12. Dim tablename As String = ""
    13. If dtschema.Rows.Count > 0 Then
    14.     tablename = dtschema.Rows(0).Item("TABLE_NAME").ToString()
    15. Else
    16.     Exit Sub
    17. End If
    18.  
    19. Dim cmd As New OleDbCommand(String.Format("SELECT * FROM [{0}];", tablename), conn)
    20.  
    21. Dim dtData As New DataTable
    22. Dim da As New OleDbDataAdapter(cmd)
    23.  
    24. da.Fill(dtData)

    Everything works fine except one thing.

    The headers of the table I'm trying to get are on the second row, not on the first one. It looks like this:

    Code:
          |    A    |    B    |    C    |    D    | ...
    ------|---------+---------+---------+---------+     
         1|This is a main table title which spans the whole table
    ------|---------+---------+---------+---------+     
         2| Header1 | Header2 | Header3 | Header4 | ...
    ------|---------+---------+---------+---------+
         3| Data    | Data    |  Data   |  Data   |
    ------|---------+---------+---------+---------+     
    ...
    Now, when I get that table the headers are treated as data while the table title from the first row is considered a header. Is there a workaround or I just have to live with it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width