0

在这里,我有我的 vb6 代码,可以使用 excel 文件。(我从其他网站上取下了这段代码。)这段代码编译和构建,但是当我点击 f5 时,它确实没有弹出表单。如果我删除类中的所有代码,它就会弹出。

我将如何解决这个问题?
抱歉,我是 vb6 的新手,但一般不会编码。

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Dim xl As New Excel.Application
    Dim xlsheet As Excel.Worksheet
    Dim xlwbook As Excel.Workbook

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'the benifit of placing numbers in (row, col) is that you
        'can loop through different directions if required. I could
        'have used column names like "A1" 'etc. 

        'Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1
        'Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 

        'don't forget to do this or you'll not be able to open
        'book1.xls again, untill you restart you pc.
        xl.ActiveWorkbook.Close(False, "f:\a.xls")
        xl.Quit()
    End Sub

    Private Sub Form_Load()
        xlwbook = xl.Workbooks.Open("f:\a.xls")
        xlsheet = xlwbook.Sheets.Item(1)
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
        xlwbook = Nothing
        xl = Nothing
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        xlsheet.Cells(2, 1) = "adsfasdfasdf"
        xlsheet.Cells(2, 2) = "qwerqwer"
        xlwbook.Save()

        'don't forget to do this or you'll not be able to open
        'book1.xls again, untill you restart you pc.
        xl.ActiveWorkbook.Close(False, "f:\a.xls")
        xl.Quit()
    End Sub

End Class
4

2 回答 2

2

那么这意味着您没有添加对项目的引用

按照链接到代码和更多详细信息..

http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm

于 2013-06-02T15:01:00.117 回答
0

仔细检查文件名、路径和扩展名。

     ("f:\a.xls")

扩展名可能是 xlsx。

还要检查工作表 1 的名称。

于 2013-06-02T08:42:40.930 回答