Private Sub excelsaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles excelsaveBtn.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 Microsoft.Office.Interop.Excel.Application Dim book As Microsoft.Office.Interop.Excel.Workbook Dim sheet As Microsoft.Office.Interop.Excel.Worksheet 'create a workbook and get reference to first worksheet xls.Workbooks.Add() book = xls.ActiveWorkbook sheet = CType(book.Sheets(1), Excel.Worksheet) '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 lvw.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() End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)