How to quickly export the database as an Excel document (another method)

zhaozj2021-02-16  52

The way to export to the Excel document is probably like this.

Dim i As Integer, j As IntegerDim myexcel As New Excel.ApplicationDim mybook As New Excel.WorkbookDim mysheet As New Excel.WorksheetSet mybook = myexcel.Workbooks.Add 'add a new BOOKSet mysheet = mybook.Worksheets.Add' add a new 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.saves (m_excelname)' Save File

This method has no mistake, but if the amount of data is large, trouble is coming, the program is not responding for a long time.

The key is loop, give my code.

Suppose the defined record named Myres

Dim myexcel As New Excel.ApplicationDim mybook As New Excel.WorkbookDim mysheet As New Excel.WorksheetSet mybook = myexcel.Workbooks.Add 'add a new BOOKSet mysheet = mybook.Worksheets.Add' add a new SHEET myexcel.visible = true

MySheet.cells.copyFromRecordset Myres

Mybook.saves (m_excelname) 'Save File

Using this code, it can be greatly shortened, and more than 8,000 records need to be more than 2 points.

The second method is only about 4 seconds, and you can experiment in person, the above results test platform is Win98 Excel2000

转载请注明原文地址:https://www.9cbs.com/read-21910.html

New Post(0)