您的位置:首页 > 编程语言 > VB

VB输出数据到EXCEL

2016-04-22 14:50 435 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: