good day..
i have a slight problem in my enrolment sytem.
im new in vb programming, and this problem perfectly work in
stressing out and pressuring me as begginer.

i have a listbox that contains all filtered data from my database.
and i want it (all entry in the filtered listbox) to be exported to ms excel. i tried searching on different forums
but it doesnt resolved my problem. im using visual studio 2012 by the way.
can somebody could run me through this problem?

Here's an example that I wrote up that uses a listview. It should be easy to modify it to iterate through a ListBox

Imports Microsoft.Office.Interop
Imports System.Windows.Forms

'                                                                                       
'  Name:                                                                                
'                                                                                       
'    ExcelExportListView                                                                
'                                                                                       
'  Description:                                                                         
'                                                                                       
'    Sample code to show how to open and manipulate an Excel spreadsheet from within a  
'    VB application.                                                                    
'                                                                                       
'  Notes:                                                                               
'                                                                                       
'    It is very important to not reference objects directly through other objects. This 
'    is often referred to as the double-dot restriction. For example, when you create   
'    a local variable to refer to the active worksheet, first create a local variable   
'    to refer to the active workbook, then generate the worksheet reference through     
'    that variable. That is do                                                          
'                                                                                       
'       Dim book As Excel.Workbook = xls.ActiveWorkbook                                 
'       Dim sheet As Excel.Worksheet = book.ActiveSheet                                 
'                                                                                       
'    rather than                                                                        
'                                                                                       
'       Dim sheet As ExcelWorksheet = xls.ActiveWorkbook.ActiveSheet                    
'                                                                                       
'    If you do the latter then you will end up with one orphan Excel.exe task for each  
'    time you run the application.                                                      
'                                                                                       
'  Audit:                                                                               
'                                                                                       
'    2013-06-20  rj  original code                                                      
'                                                                                       

Public Class Form1

    Private Sub btnPopulate_Click(sender As System.Object, e As System.EventArgs) Handles btnPopulate.Click

        'Add a few test items to the listview

        lvwToExport.Items.Add(New ListViewItem({"one", "two", "three"}))
        lvwToExport.Items.Add(New ListViewItem({"four", "five", "six"}))
        lvwToExport.Items.Add(New ListViewItem({"seven", "eight", "nine"}))
        lvwToExport.Items.Add(New ListViewItem({"ten", "eleven", "twelve"}))
        lvwToExport.Items.Add(New ListViewItem({"thirteen", "fourteen", "fifteen"}))

    End Sub

    Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click

        'Export the listview to an Excel spreadsheet

        SaveFileDialog1.Title = "Save Excel File"
        SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.xlsx)|*.xslx"
        SaveFileDialog1.ShowDialog()

        'exit if no file selected

        If SaveFileDialog1.FileName = "" Then
            Exit Sub
        End If

        'create objects to interface to Excel

        Dim xls As New Excel.Application
        Dim book As Excel.Workbook
        Dim sheet As Excel.Worksheet

        'create a workbook and get reference to first worksheet

        xls.Workbooks.Add()
        book = xls.ActiveWorkbook
        sheet = book.ActiveSheet

        'step through rows and columns and copy data to worksheet

        Dim row As Integer = 1
        Dim col As Integer = 1

        For Each item As ListViewItem In lvwToExport.Items
            For i As Integer = 0 To item.SubItems.Count - 1
                sheet.Cells(row, col) = item.SubItems(i).Text
                col = col + 1
            Next
            row += 1
            col = 1
        Next

        'save the workbook and clean up

        book.SaveAs(SaveFileDialog1.FileName)
        xls.Workbooks.Close()
        xls.Quit()

        releaseObject(sheet)
        releaseObject(book)
        releaseObject(xls)

    End Sub

    Private Sub releaseObject(ByVal obj As Object)

        'Release an automation object

        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try

    End Sub

End Class

thank you for the reply.. im working it now base on your sample.. :)

For Each item As ListViewItem In lvwToExport.Items

            For i As Integer = 0 To item.SubItems.Count - 1


            sheet.Cells(row, col) = item.SubItems(i).Text
            col = col + 1
        Next
        row += 1
        col = 1
    Next

i got an error in this line.. sheet.Cells(row,col) <---- expression is a value and therefore cannot be the target of an assignment..

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.