Read Excel Column Headers with VS VB.NET

This is a discussion on Read Excel Column Headers with VS VB.NET within the ADO DAO RDO RDS forums in Framework and Interface Programming category; I have been given what seems a simple task, but in developing the app, it's turning out to be not quite so simple... I am writing this app as a WinForm in VB.NET VS2005. Here's some pseudo-code for what my app has to do... 1. User selects an Excel Spreadsheet 2. I read the Excel Spreadsheet Column Headers 3. Populate a Listbox with those column headers. That's it... Sounds simple, yes? Well, here's the problem - the Excel Spreadsheet could be anything from Excel 95 right through Excel 2007 (eg, 5.0 - 12.0). I have tried a bunch of techniques ...

Go Back   Application Development Forum > Framework and Interface Programming > ADO DAO RDO RDS

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 11-10-2008, 10:37 AM
Beebo
Guest
 
Default Read Excel Column Headers with VS VB.NET

I have been given what seems a simple task, but in developing the app, it's
turning out to be not quite so simple... I am writing this app as a WinForm
in VB.NET VS2005.

Here's some pseudo-code for what my app has to do...

1. User selects an Excel Spreadsheet
2. I read the Excel Spreadsheet Column Headers
3. Populate a Listbox with those column headers.
That's it...

Sounds simple, yes? Well, here's the problem - the Excel Spreadsheet could
be anything from Excel 95 right through Excel 2007 (eg, 5.0 - 12.0).

I have tried a bunch of techniques from GetOleDbSchema through Interop and
the problem seems to be that, depending on the Excel version, there is no
"one" technique for getting those headers which means I would have possibly
tons of code, all based on what version of Excel is being used.

Would anyone know of a clean, easy way to do this for any (even most)
versions?

I would really appreciate some help and advice on this one - its becoming a
real headache!!!

Thanks!


Reply With Quote
  #2  
Old 12-10-2008, 10:09 AM
Junior Member
 
Join Date: Dec 2008
jeral01 is on a distinguished road
Wink Re: Read Excel Column Headers with VS VB.NET

Try this:

Private Sub readfromExcel()
Dim a As String

cn = New System.Data.OleDb.OleDbConnection("provider=Micros oft.Jet.OLEDB.4.0;" & _
"data source=" & FileName & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'")

' Select the data from Sheet1 of the workbook.
cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", cn)

cn.Open()
cmd.Fill(ds, "ExcelData")

MaxRows = ds.Tables("ExcelData").Rows.Count
MaxCols = ds.Tables("ExcelData").Columns.Count
cn.Close()

lv1.Items.Clear()
lv1.View = View.Details
lv1.Columns.Clear()

' Column Headers ...
For k = 1 To MaxCols
a = ds.Tables("ExcelData").Columns(k - 1).ToString
lv1.Columns.Add(a, 90)
Next

' Row data ...
For i = 1 To MaxRows
For k = 1 To MaxCols
str(k - 1) = ds.Tables("ExcelData").Rows(i - 1).Item(k - 1).ToString '
Next
itm = New ListViewItem(str)
lv1.Items.Add(itm)
Next

End Sub

Cheers!!!
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 12:38 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.