DBGRID uses a book (four)

xiaoxiao2021-03-06  64

DbGrid title bar popup menu control procedure TFrmOrderPost.DbgOrderPostMouseDown (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var CurPost: TPoint; begin GetCursorPos (CurPost); // get the current mouse coordinates if ( Y <= 17) and (x <= vcurrect.right) The begin if button = mbright the begin pmtitle.popup (curpost.x, curpost.y); end; end; end; // vcurRect This variable is DBGRID DrawColumnCell events obtained {procedure TFrmOrderPost.DbgOrderPostDrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin vCurRect: = Rect; // vCurRect defined in the implementation section end;} how several The content in dbgrid is imported 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 application:

Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, DB, DBTables, Grids, DBGRIDS, ActiveX, Comobj, Excel2000, Oleserver;

test environment:

OS: Win2k Pro; Excel2k; Delphi6.0

Source program:

{Function Description: Output DBGRID to Excel Form (Support Multi Sheet) Design: Coolslob Date: 2002-10-23 Support: Coolslob@163.com Call Format: CopyDbDataToExcel ([DBGRID1, DBGRID2]);}

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; xlapp.sheetsinnewwbook: = high (args) 1;

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 dam screen.cursor: = crdefault; EXIT;

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; TDBGRID (args [i] .vobject) .datasource.DataSet.Next; end;

XLapp.visible: = true; screen.cursor: = crdefault;

How to achieve automatic numbers like Excel on the left side of DBGRID? These numbers are not related to the table unrelated unit unit1;

Interface

Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGRIDS, STDCTRLS, Buttons, DB, DBTables, ExtCtrls, JPEG; Const Rowcnt = 20;

type tmygrid = class (tdbgrid) protected procedure Paint; override; procedure DrawCell (ACol: Integer; ARow: Integer; ARect: TRect; AState: TGridDrawState); override; public constructor create (AOwner: TComponent); override; destructor destroy; override ;

TFORM1 = Class (TFORM) Bitbtn1: Tbitbtn; DataSource1: TDataSource; Table1: Ttable; Procedure Bitbtn1click (Sender: Tobject); private {private declarations}.

Var Form1: TForm1; MyGrid: TMYGRID; Implementation {$ r * .dfm}

{TMYGRID} Constructor TMYGRID.CREATE (AOWNER: TComponent); Begin inherited Create (OWNER); ROWCOUNT: = rowcnt;

DESTRUCTOR TMYGRID.DESTROY;

Procedure tmygrid.paint; begin rowcount: = rowcnt; if Dgindicator in Options Ten ColWidths [0]: = 30; inherited;

procedure tmygrid.DrawCell (ACol: Integer; ARow: Integer; ARect: TRect; AState: TGridDrawState); begin inherited; if (ARow> = 1) and (ACol = 0) then Canvas.TextRect (ARect, ARect.Left, ARect .Top, inteltostr (aro);

Procedure TFORM1.Bitbtn1click (Sender: TOBJECT); begin mygrid: = tmygrid.create (self); mygrid.parent: = self; mygrid.Left: = 0; mygrid.top := 0; mygrid.height: = 300; myGrid .Datasource: = datasource1;

End.

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

New Post(0)