Several people have asked how to export data to Excel. This code snippet shows how to export the data from a listview in details mode to a new Excel spreadsheet. Take note of the comments in the header to avoid having orphaned Excel.exe tasks eat up your system memory.

Begginnerdev commented: Nice post Jim. +9
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"

        '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

        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
            row += 1
            col = 1

        'save the workbook and clean up



    End Sub

    Private Sub releaseObject(ByVal obj As Object)

        'Release an automation object

            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try

    End Sub

End Class
About the Author
Member Avatar Reverend Jim Hi, I'm Jim, one of DaniWeb's moderators.

I completed my Computer Science degree at the University of Manitoba in 1976. I did two and a half years of programming in medical research followed by twenty-nine years at Manitoba Hydro (electric utility). Most of that was spent on doing development and maintenance on an AGC/SCADA (real-time programming/process control) system. The last ten years of that was spent doing application and infrastructure support and development. I have programmed in FORTRAN (mostly), APL, PL/1, COBOL, Lisp, SNOBOL, ALGOL, Assembler (several flavours), C, C++, Paradox, VB, vbScript and more recently, Python. I am married with two grown children of whom I am very proud, and a most beautiful wife. I am currently retired (and loving it).




good job, thanks


Thanks for the code, but how can I add the Headers to the exported file?

Reverend Jim 3,174

You should be able to figure that out from the code. You can take

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

and put headers/text in any cell you want. Just change the values for row and col to select a specific cell and replace


with the string you want in the header cell.


hi, Jim

Thanks for the great work you've done for the forum.Exactly what I need.
If I got any other problem ,I'll get back to you.


Hello again, thanks for the help. It works fine :)
But I have another question, as u can see in the picture some numbers are not shown as they should. You have an idea why its so?
Thanks again. !


Reverend Jim 3,174

The link to the picture is broken.



got it
replace this


with this


mark jayson

what is the reference needed to apply the Excel.application??

ddanbe 2,724

@mark jayson
I'm assuming you are using VS2015. You won't find the reference Jim mentioned here, just add the reference MicroSoft Office 16,0 Object Library and as by wonder, Jim's reference is there again.

Mark Adrian

Thanks! Big help! :)


how to add title or caption in exported data in listview, example

items in listview
                                                                                                                 name and  Signature


how about another listview will exported to sheet2

Reverend Jim 3,174

I no longer have MS Office installed so I can't test an example to post. Anyone else?