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 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
时间: 2024-11-11 16:29:32