Dim i As Integer, j As Integer
Dim myexcel As New Excel.Application
Dim mybook As New Excel.Workbook
Dim mysheet As New Excel.Worksheet
Set mybook = myexcel.Workbooks.Add ''添加一个新的BOOK
Set mysheet = mybook.Worksheets.Add ''添加一个新的SHEET
'' For i = 1 To myres.RecordCount
'' For j = 1 To myres.Fields.Count
'' mysheet.Cells(i, j) = myres.Fields.Item(j - 1).Value
'' If (i * j) Mod 500 = 0 Then
'' DoEvents
'' End If
'' Next j
'' myres.MoveNext
'' Next i
myexcel.Visible = True
mybook.SaveAs (m_ExcelName) ''保存文件
这中方法没什么错误,但是如果数据量很大的话,麻烦就来了,出现程序长时间不响应
关键是循环,下给出我的代码
假设定义的记录名为myres
Dim myexcel As New Excel.Application
Dim mybook As New Excel.Workbook
Dim mysheet As New Excel.Worksheet
Set mybook = myexcel.Workbooks.Add ''添加一个新的BOOK
Set mysheet = mybook.Worksheets.Add ''添加一个新的SHEET
myexcel.visible=true
mysheet.Cells.CopyFromRecordset myres
mybook.SaveAs (m_ExcelName) ''保存文件
利用这个代码可以大大的缩短导出时间,8000多个纪录用第一种方法大概需要2分多
而用第二种方法只大概要4秒,可以亲自试验一下,上述结果测试平台为WIN98+EXCEL2000