|
-
Apr 21st, 2009, 08:35 AM
#1
Thread Starter
Hyperactive Member
Loading data from excel and calling by columns as fieldname
Hello everyone this is me again. I am having a problem regarding to Importing file from excel to listview. Actually my problem is that my program will let the user define what database he/she want. I know already how to load on my listview from .txt, access, ms sqlserver, but my problem is the user want to open from excel file. Base from his data, the columns contained Contact, address, age,name and only in sheet1. My problem now is that the user want to call his/her excel act like a database. The tablename will be Sheet1 and if he/she want to display the age and name column he/she can view by calling the its columnname. The columname will act as a fieldname. How can I do this upon loading all of this to listview. I have here an example on how i load data from .txt(csv) format but i need an excel on how to load data coming from excel. Here is the ff.
Code:
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mFolder & "\;Extended Properties='text;HDR=Yes'"
CommandText = "select * from " & myString & "
conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)
conn.Open()
Dim t As New DataTable
' initializing its column to complement on how many fields I want to return in my query command
t.Columns.Add("F1")
t.Columns.Add("F2")
t.Columns.Add("F3")
' a method to access read-only the result set.
Dim reader As OleDbDataReader = Command.ExecuteReader()
While reader.Read()
' create new row
Dim r As DataRow = t.NewRow()
r(0) = reader("F1")
r(1) = reader("F2")
r(2) = reader("F3")
' add a row to a datatable
t.Rows.Add(r)
End While
' close reader
reader.Close()
' close the connection
conn.Close()
For i As Integer = 0 To t.Rows.Count - 1
Dim li As ListViewItem = frmMain.ListView1.Items.Add(t.Rows(i)("F1").ToString())
li.SubItems.Add(t.Rows(i)("F2").ToString())
li.SubItems.Add(t.Rows(i)("F3").ToString())
Next
Thanks in advance and have a nice day.
-
Apr 21st, 2009, 11:23 AM
#2
Re: Loading data from excel and calling by columns as fieldname
The steps for reading data from an excel file are identical to those of a csv file using ado.net. The only different is the connection string. Look it up at connectionstrings.com.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|