In many times, we need to export our data into Excel for processing, and ready-made tools in Access can be implemented in Delphi, no matter what we need such a tool, then, things are not suitable, fine I think, the best way is to generate an Excel file directly in the program in the program. Use the OLE technology to directly call the Excel instance. Since the first method technology is too complicated, it is installed on a general computer. In this regard, the second method "throws a piece of brick". Being this way to make it easy for convenience, we have considered that there are many cases that export data is Master / Detail data, so use two TQuery and a TDataSource, if there are more levels, just add TQuery and TDataSource, The following uses two TQuery, named: QRYMASTER and QRYDETAIL, a TDataSource: DSSource; step is: 1. Create an Excel instance 2. Create a worksheet 3. Create a TSTRINGLIST, use to load data 4. Vital data table To store data into TStringList 5. Copy the data of TStringList to the clipboard 6. The advantage of paste the data in the clipboard to the Excel this method is that the speed of the excel is greatly improved, I have tried it, I have tried it. In the machine of P3933, 256M, it takes about 2 minutes to export 1000 data for about 2 minutes, and only 8 seconds after this method.
Function toExcel (): boolean; var Y: integer; s: string; asheet: variant; begin result: = true; Excel.connect; // Open Excel Excel.visible [0]: = true; // Show Excel Excel.Workbooks.add (xlwbatworksheet, 0); asheet: = Excel.Worksheets.Item [1];
Tslist: = tstringlist.create; try, qrymaster do begin open; first; while not eof do beg, s: = '; for y: = 0 to fieldcount-1 do begin s: = s fields [y] .sstring # 9; Application.ProcessMessages; End; Tslist.add (s); // From Table IF Qrydetail <> nil dam with qrydetail do begin open; first; while not EOF do bas s: = '; for y: = 0 TO FIELDCOUNT-1 Do Begin S: = S Fields [Y] .sstring # 9; Application.Pro CESSMESSAGES; end; tslist.add (s); next; end; end; end; next; end; close; end; clipboard.astext: = tslist.text; eXcept results: = false; end; finally tslist.free; end; fin or tslist.free; ;
Excel.disconnect; asheet.paste; messagebox (application.handle, 'data export!', 'System prompt', MB_ICONITIONORMATION or MB_OK); END; Author Related Works: ◢█████◤ ◢◤ ◢◤◣ ◢ ◤ ◤ ◢◤█ ◣ ◢◤ ◢◤█ ◥◣◢◤ 丽 ◢◤█ ◥◤ ◢◤ ◢◤ 软 ◢◤ http: //wosens.com ◢◤ ◢◤ 件 ◢◤ ◥██████ ◢◤ ◥◣ ████◤ ◥███ ======== 小 子 娱乐 娱乐 ======== "Small Swallow Trust" "Small Swallows" "Small Swallow Square"
======== Programmer toolbox series ======== "Memory cleaning machine" "Cool Query: Cool Query" "Password King: Passwordking" "Ling Li Mail Group" "SQL Database Generator" "Web Mixer" "WinRoute Log Analysis" "Diaries Written"
======== Invincible Business Series ======== "Invincible Letter" "" Invincible Image Seal "