vb怎么把数据输出到excel里面,有大量的数据?

vb怎么把数据输出到excel里面,有大量的数据?,第1张

最简单的办法:

open "a.xls" for output as #1

print #1, "姓名", vbTab,"学号", vbTab,"总分"

print #1, "张三", vbTab,1, vbTab,88

print #1, "李四", vbTab,2, vbTab,99

close #1

这样的文件可以双击在EXCEL里面打开,但是不能设置复杂的格式。

Option Explicit

Private Sub Form_Load()

Dim i As Long, j As Long

Me.MSHFlexGrid1.Rows = 2000

Me.MSHFlexGrid1.Cols = 10

For i = 0 To Me.MSHFlexGrid1.Rows - 1

For j = 0 To Me.MSHFlexGrid1.Cols - 1

Me.MSHFlexGrid1.TextMatrix(i, j) = i &"行" &j &"列"

Next

Next

Debug.Print Me.MSHFlexGrid1.TextArray(100)

End Sub

Private Sub cmdExport_Click()

Dim i As Long, j As Long

Dim CellsData() As String

Dim objApp As Excel.Application

Dim objWorkbook As Excel.Workbook

Dim objWorksheet As Excel.Worksheet

Dim objRange As Excel.Range

'构造二维数组

ReDim CellsData(1 To Me.MSHFlexGrid1.Rows, 1 To Me.MSHFlexGrid1.Cols)

For i = 1 To Me.MSHFlexGrid1.Rows

For j = 1 To Me.MSHFlexGrid1.Cols

CellsData(i, j) = Me.MSHFlexGrid1.TextMatrix(i - 1, j - 1)

Next

Next

'导出到Excel中

Set objApp = New Excel.Application

objApp.ScreenUpdating = False '禁止屏幕刷新

Set objWorkbook = objApp.Workbooks.Add

Set objWorksheet = objWorkbook.Sheets.Add

Set objRange = objWorksheet.Range(objWorksheet.Cells(1, 1), objWorksheet.Cells(Me.MSHFlexGrid1.Rows, Me.MSHFlexGrid1.Cols))

objRange.Value = CellsData

objApp.Visible = True

objApp.ScreenUpdating = True

'销毁二维数组

Erase CellsData

Me.SetFocus

MsgBox "导出完毕"

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8076238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存