Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i get the above error for this code which i am using to save listview data as excel sheet.


VB
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
Posted

1 solution

Did you add the References for the Interop Assemblies?
 
Share this answer
 
Comments
Maciej Los 15-Feb-15 17:43pm    
Is it an answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900