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.

Nice post Jim.
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

Computer languages in which I have developed applications

Assembler (DEC, Data General, 8080, GE, SEL, IBM 360)
Borland Paradox

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. !


Attachments excel.PNG 311.16 KB

@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.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.