Import SQL data to an Excel worksheet

Source: Internet
Author: User

Fast import SQL records to the Excel table, 19 sub-section 50 thousand records only 30 seconds http://expert.csdn.net/Expert/topic/1035/1035148.xml? Temp =. 9380762.

I can see a program on the Internet. I didn't use VBA programming to import SQL data into an Excel table. It is very fast.

The main idea is to use Excel as a database. Its name is the database name, And the worksheet is a table in the database.
Create a project, reference Dao, add command1, and paste the code
'Declare API functions
Private declare function findwindow lib "USER32" alias "find0000wa" (byval lpclassname as string, byval lpwindowname as string) as long
'Define variables

Private sub commandementclick ()
'If the Excel file has been opened, close it first.

Dim lpclassname as string
Dim lpcaption as string
Dim handle as long
Lpclassname = "xlmain"
Lpcaption = "Microsoft Excel-myexcel.xls"
Handle = findwindow (lpclassname $, lpcaption $)
If handle <> 0 then
Msgbox "Close the Excel file first! ", Vbokonly + vbinformation," You cannot write an opened file! "
Exit sub
End if
'Check whether the Excel file exists. If yes, delete it.
If Dir (App. Path & "/myexcel.xls") <> "" Then kill app. Path & "/myexcel.xls"
'For Data Conversion
Dim dbs as database
'Open the database
Set DBS = opendatabase ("", false, false, "ODBC; DSN = IDMS; database = IDMS; uid = sa; Pwd =;") 'connection string, modify as needed
'Import data to excel
DBS. Execute "select" & "personid as resident number, name as name, sex as gender ,"&_
"Birthday as birthdate, nation as nationality, nativeplace as nationality ,"&_
"Politics as political outlook, idcard as ID card number, study as education ,"&_
"Workplace as work unit, workphone as phone, homephone as home phone ,"&_
"Mobilephone as mobile phone or bpserver, carcard as license plate number, startdate as Check-in date ,"&_
"Patch as area, tianmentid as apartment number, unitno as unit number ,"&_
"Roomid as room no., contractid as House Purchase Contract No." & "into [Excel 8.0; database =" & App. Path & "/myexcel.xls]. [worksheet1] From" & "tbl_tenement"
'Close database objects
DBS. Close
'Release database objects
Set DBS = nothing
'Open the generated Excel table by calling EXCEL.
Shell "D:/program files/Microsoft Office/office10/Excel. EXE" & App. Path & "/myexcel.xls", vbmaximizedfocus

End sub

It takes only 30-60 seconds to write 50 thousand records in 19 fields and 20 thousand records using VBA. It takes 83 minutes to write records directly to the cell. The speed is limited to times, the younger brother's level is limited and cannot be solved. I hope you will discuss and improve it.

1. This code may immediately see the "sorting method not supported by the system" error. After resume next is added, it will be resolved. What is the problem caused by this? Can you eliminate it.
2. How many records can be stored in an Excel file? When I test the storage of 100,000 records, the following error occurs: "The workbook is full"

You are welcome to discuss and let all the friends who are troubled by the speed of importing data to excel see this code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.