| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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! |
|
#2
| |||
| |||
| 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!!! |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.