我想做一个程序:程序中调入excel,下图中文本中写入内容,按确定他会添加到excel中,且界面上的excel就会添入数据。然后到网上下了一个可以显示的,但是有好多不符合,首先运行后excel能显示出来但是excel上面的性别,名字不知从哪里来的代码如下:
Option Explicit
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类Private Sub Command1_Click()
    Dim i As Integer, j As Integer
    Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
    xlApp.Visible = False '设置EXCEL不可见
    Set xlBook = xlApp.Workbooks.Open(App.Path & "\data.xls") '打开EXCEL工作簿
    Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
    'xlsheet.Activate '激活工作表
    Dim litem
    For i = 2 To 7 '行/记录条数        Set litem = ListView1.ListItems.Add()
        litem.Text = xlsheet.Cells(i, 1)
        litem.SubItems(1) = xlsheet.Cells(i, 2)
        litem.SubItems(2) = xlsheet.Cells(i, 3)
        litem.SubItems(3) = xlsheet.Cells(i, 4)
        'MsgBox xlsheet.Cells(i, j)    Next
    xlBook.Close (True) '关闭EXCEL工作簿
    xlApp.Quit '关闭EXCEL
    Set xlApp = Nothing '释放EXCEL对象End Sub

解决方案 »

  1.   

    Option Explicit
    Dim xlApp As Excel.Application '定义EXCEL类
    Dim xlBook As Excel.Workbook '定义工件簿类
    Dim xlsheet As Excel.Worksheet '定义工作表类
    private sub form_load()
        Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
        xlApp.Visible = true '设置EXCEL可见
        Set xlBook = xlApp.Workbooks.add
        Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
        'xlsheet.Activate '激活工作表
    end sub
    Private Sub Command1_Click()
       ''给第一个单元格赋值
        xlsheet.Cells(1, 1)="sofhoisfhoisf"
    End Sub
    private form_unload()
    set xlsheet=nothing
    set xlbook=nothing
    xlapp.quit
    set xlapp=nothing
    end sub