Http://dev.9cbs.net/Article/53/53442. How to import the contents of several DBGRID into the same Excel table? In the actual production of software, in order to save development costs and development cycles, some software personnel usually export directly in DBGRID to the Excel table, and the previously seen function can only import data in one sheet of Workbook, Do not support more Sheet! .
~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
Unit Applications: Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, DB, DBTABLES, GRIDS, DBGRIDS, ACTIVEX, COMOBJ, Excel2000, Oleserver;
procedure CopyDbDataToExcel (Args: array of const); var iCount, jCount: Integer; XLApp: Variant; Sheet: Variant; I: Integer; begin Screen.Cursor: = crHourGlass; if not VarIsEmpty (XLApp) then begin XLApp.DisplayAlerts: = False; xlapp.quit; varclear (xlapp);
Try xlapp: = createoleObject ('Excel.Application'); Except Screen.cursor: = Crdefault; EXIT;
XLapp.Workbooks.add; // (1) xlapp.sheetsinnewwbook: = high (args) 1; // (2) ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ high (Args) 1; XLApp.WorkBooks.Add; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for i: = low (args) to high (args) do begin xlapp.workbooks [1]. Worksheets [i 1] .name: = TDBGRID (args [i] .vobject) .name; Sheet: = xlapp.workbooks [1] .Worksheets [TDBGRID (args [i] .vobject) .name];
If not tdbgrid (args [i] .vobject) .datasource.DataSet.Active the begin screen.cursor: = crdefault;
TDBGRID (args [i] .vobject) .datasource.DataSet.first; for iCount: = 0 to tdbgrid (args [i] .vobject) .COLUMNS.COUNT - 1 Do Sheet.cells [1, ICOUNT 1]: = TDBGRID (args [i] .vobject) .COLUMns.Items [iCount] .title.caption;
JCOUNT: = 1; While Not TdbGrid (args [i] .vobject) .datasource.dataset.eof do begin for iCount: = 0 to TDBGRID (args [i] .vobject) .COLUMNS.COUNT - 1 Do Sheet.cells [ JCOUNT 1, ICOUNT 1]: = TDBGRID (Args [i] .vobject) .Columns.Items [iCount] .field.Asstring; inc (jcount); tdbgrid (args [i] .vobject) .datasource.DataSet. Next; end;
XLapp.visible: = true; screen.cursor: = crdefault; end; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ After the test, the above code does have problems: (interested friends can test it yourself)
For example: first form1.copydbdattoExcel ([DBGRID1, DBGRID2, DBGRID3]); // OK re-form1.copydbdatatoExcel ([DBGRID1, DBGRID2, DBGRID, DBGRID4]); // This is wrong, prompt: invalid index
If this is true: first form1.copydbdattoExcel ([DBGRID1, DBGRID2, DBGRID3, DBGRID4]); // ok re-form1.copydbdatatoExcel ([DBGRID1, DBGRID2]); // ok
~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ Summary: After a few more, you will make mistakes ... The reason is in the code (1), (2) two paragraphs After testing, the correct one should be called (1), (2) code, to ensure that it is not wrong ....