i HAVE an excel file . and on sheet 1 i have some data in column A.

i want to all the data from column A to list box 1 on form 1 in visual basic 2008 but how?

Regards
Mahaveer

Recommended Answers

All 10 Replies

thanks brother,

i saw your link but i want to get column A data on Listbox1

Once you know how to extract column A from the excel sheet it should be a simple matter to insert the values into a listbox.

dragon

but i have no knowledge how to get column A data to list box 1 on form 1
can you help me to get it please.

dragon

i tried to pull data in list box 1 and i do it.....really

but i m getting a little problem

i m getting 0,0,0,0,0 in my listbox1 but
i want it like this:
0
0
0
0
0
what i do?

vb.net can't read the excel file directly -- use Excel Automation, which launches Excel to do it for you. There are lots of tutorials if you just google for "vb.net excel automation". It assumes Excel is installed on the computer. If it isn't, then I think you are SOL.

Here is a more in-depts explanation which you might want to read.

The only other alternative I know about is to have Excel export the file into a CVS format text file, which means each cell on a row is separated with commas or tabs. Then your vb.net program can just read it as normal text.

Here is an example I worked up,

Imports Microsoft.Office.Interop

Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
        Dim oExcel As Object = CreateObject("Excel.Application")
        Dim oBook As Object = oExcel.Workbooks.Open("c:/Book1.xlsx")
        Dim oSheet As Object = oBook.Worksheets(1)          
        Dim i As Integer
        Dim cell As String
        For i = 0 To 10
            'set cell name, e.g. A1, A2, etc
            cell = "A" & Convert.ToString(i + 1)
            ' get cell data from Excel
            cell = oSheet.Range(cell).Value
            ' add the data to Listbox1
            ListBox1.Items.Add(cell)
        Next
        oExcel.Quit()

    End Sub

End Class

Thank you very much dragon,

But i have a minor problem with your code,

in your code you said for i = 0 to 10

but suppose that i have value in A1 to A5 and A7 to A10. means i have no value in A6 then it is not working.

Regards
CA Mahaveer Somani

i used the following:

' add the data to Listbox1
            If cell = Nothing Then
                ListBox1.Items.Add("")
            Else
                ListBox1.Items.Add(cell)
            End If

and i did it .........thank you very much dragon....but now i m trying to add coloumn B and C also like A.

and i want to add the last all the used cell not only upto 10.

i means if column A is used upto 50 then 50 items should be add not only 10 but how to use "lastcell" in your code....

Regards
CA Mahaveer Somani

Here is the information you need about Microsoft Excel Object Model. I don't know the answer to your question without research, and you can research just as well as I can.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.