VB输出数据到EXCEL

Private Sub Command1_Click()
    Dim i As Long
    Dim j As Long
    Dim myData(10, 10) As Long
    Dim xlApp, WS, WB
    Set xlApp = CreateObject("Excel.Application")
    Set WB = xlApp.WorkBooks.Add
    Set WS = WB.Sheets(1)
    For i = 0 To 10
        For j = 0 To 10
            WS.Cells(i + 1, j + 1).Value = myData(i, j) '写入第i+1行,第j+1列
        Next j
    Next i
    xlApp.Visible = True
    Set xlApp = Nothing
    Set WS = Nothing
    Set WB = Nothing
End Sub

上面只是读取出来,如果要写入操作,也可以:

Private Sub Command1_Click()
    Dim i As Long
    Dim j As Long
    Dim myData(10, 10) As Long
    Dim xlApp
    Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
    xlApp.WorkBooks.Open ("d:\123.xls")
    xlApp.WorkSheets("Sheet1").Activate
    For i = 0 To 10
        If i > 5 Then xlApp.WorkSheets("Sheet2").Activate
        For j = 0 To 10
            xlApp.Cells(i + 1, j + 1).Value = myData(i, j) '写入第i+1行,第j+1列
        Next j
    Next i
    '保存
    'xlApp.ActiveWorkbook.Save
    '另存
    'xlApp.ActiveWorkbook.SaveAs "d:\456.xls"
    '显示出来
    'xlApp.Visible = True
    '关闭
    'xlApp.WorkBooks.Close
    'xlApp.Quit
    Set xlApp = Nothing
End Sub

 

posted @ 2017-07-21 06:16  JustXIII  阅读(1323)  评论(0编辑  收藏  举报