Output DBGrid to an Excel table (multiple sheet supported)

Source: Internet
Author: User
{
Function Description: outputs DBGrid to an Excel table (multiple sheet is supported)
Call format: copydbdatatoexcel ([dbgrid1, dbgrid2]);
}
ProcedureCopydbdatatoexcel (ARGs: Array  Of  Const);
VaR
Icount, jcount: integer;
Xlapp: variant;
Sheet: variant;
I: integer;
Begin
Screen. cursor: = crhourglass;
If  NotVarisempty (xlapp) Then
Begin
Xlapp. displayalerts: = false;
Xlapp. Quit;
Varclear (xlapp );
End;

Try
Xlapp: = createoleobject ('excel. application ');
Except
Screen. cursor: = crdefault;
Exit;
End;

Xlapp. workbooks. Add;
Xlapp. sheetsinnewworkbook: = high (ARGs) + 1;

ForI: = low (ARGs)ToHigh (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 NotTDBGrid (ARGs [I]. vobject). datasource. dataset. ActiveThen
Begin
Screen. cursor: = crdefault;
Exit;
End;

TDBGrid (ARGs [I]. vobject). datasource. dataset. first;
ForIcount: = 0ToTDBGrid (ARGs [I]. vobject). Columns. Count-1Do
Sheet. cells [1, icount + 1]: =
TDBGrid (ARGs [I]. vobject). Columns. items [icount]. Title. Caption;

Jcount: = 1;
While NotTDBGrid (ARGs [I]. vobject). datasource. dataset. EOFDo
Begin
ForIcount: = 0ToTDBGrid (ARGs [I]. vobject). Columns. Count-1Do
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;
End;
Screen. cursor: = crdefault;
End;

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.