Retrieve excel column data using Microsoft office pias?

  • Thread starter Thread starter murl
  • Start date Start date
M

murl

Is it just me or is there no easy way to open an excel file, and get
the column names that are usually in the 1st row? I would like to be
able to do this for a import tool im writing in vb.net and c#. I need
to be able to list what columns are available in the excel file, so
the user can map these to a ms sql table.

Dim oExcelApp As New Excel.Application()
Dim oWorksheet As Excel.Worksheet
Dim i As Integer = 0

Try
With oExcelApp
.Visible = False
.ScreenUpdating = False
End With
oExcelApp.Workbooks.Open(FilePath)

For Each oWorksheet In oExcelApp.Worksheets
Debug.WriteLine(oWorksheet.Name)
Debug.WriteLine(oWorksheet.Columns.Count - 1)
For i = 0 To oWorksheet.Columns.Count
Debug.WriteLine("Column " & i & ":" &
oWorksheet.Columns.Item(1, i).ToString)
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
oExcelApp.Workbooks.Close() : oExcelApp.Quit()
End Try
 
Hi Murl,

Look at the following artilces for various sample codes:

311731 HOW TO: Query and Display Excel Data by Using ASP.NET, ADO.NET, and
http://support.microsoft.com/?id=311731

306022 HOW TO: Transfer Data to an Excel Workbook by Using Visual Basic .NET
http://support.microsoft.com/?id=306022

301982 HOWTO: Automate Microsoft Excel from Visual Basic .NET
http://support.microsoft.com/?id=301982

The field-names that you are looking for are part of Range object not in the
column object.
Excel wokbook has fixed columns and rows and you will need to loop through all
columns using cells(1, x) in the first row yourself until you find an empty
cell assuming that they are always in the first row.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Back
Top