错误:“未定义类型Excel.worksheet” [英] error: "Type Excel.worksheet is not defined"

查看:839
本文介绍了错误:“未定义类型Excel.worksheet”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此代码的上述错误,我用它来保存listview数据作为excel表。





i get the above error for this code which i am using to save listview data as excel sheet.


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

推荐答案

你有没有? dd互操作程序集的参考文献?
Did you add the References for the Interop Assemblies?


这篇关于错误:“未定义类型Excel.worksheet”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆