Important properties and methods for Delphi Control Excel

xiaoxiao2021-03-06  61

Important properties and methods for Delphi Control Excel

There are four ways to call Excel in Delphi, and we select an important properties and methods for Delphi Control Excel to use oleObject to load the Excel worksheet.

First give some major code steps created by OLE to make a brief description:

Create an OLE object:

Var Olecon: toleContainer;

OLECON: = ToleContainer.create (Self);

Olecon.oleObject: = Olecon.createObject ('Excel.Sheet', False);

Or choose to import an Excel file to create an OLE object:

Olecon.oleObject: = Olecon.createObjectFromFile (XlsName, False);

It is best to hide several tool bars of Excel, which is like a table embedded in your program:

Olecon.oleObject.Application.commandbars ['Standard']. Visible: = false;

Olecon.oleObject.Application.commandbars ['formatting']. Visible: = false;

Olecon.oleObject.Application.commandbars ['Reviewing']. Visible: = false;

Then display and activate the Excel table, the object defined for the toleContainer:

Olecon.show;

Olecon.doverb (0);

This is basic, but toleContainer has a bad place, that is, when you click on other controls, it will lose the focus, then automatically exit, but don't really quit, just to activate it again, the key is When it loses the focus, the Excel object is invisible, you can use the TIMAGE control to cut the area picture of the toleContainer to see the area picture of Excel to deceive the user, and we are mainly not telling this here, it will not be detailed.

Let's start talking about the common attributes and methods of interfaces in Excel_TLB, mainly for some interface elements for exporting and setting report formats.

The read and write properties of the cell:

Olecon.oleObject.Application.cells.Item [1,1];

Olecon.oleObject.Application.cells (1, 1);

Olecon.oleObject.Application.cells [1,1] .value;

The above three can read and write the 'A1' unit of the worksheet.

In Delphi, the operation of all objects such as cells (set), area, and worksheet is required to be Variant.

The selected area is assigned to the Range in your own program:

Var Range, Sheet: Variant;

Range: = Olecon.oleObject.Application.range ['A1: C3'];

or:

Sheet: = Olecon.oleObject.Application.Activesheet;

Range: = Olecon.oleObject.Application.range [Sheet.cells [1,1], Sheet.cells [3,3,3]];

For the above Range merge unit:

Range.merge;

Range. Formular

1C

1: = 'Compound Area'; // After the merger is written

Note that the text in the cell to read the merged cell is the text of the cell of the upper left corner of the merge area.

The selected area is assigned to the Range: Range: = Excel_Grid1.OleObject.Application.selection;

Split cell:

Range.unmerge;

Set the format of the cell (set) after the merger:

Range.horizontalalignment: = xlcenter; // Text level homogeneous

Range.VerticalAlignment: = xlcenter // Text vertical center

Range.wraptext: = true; // Text Automatic Wrap

Range.Borders.LineStyle: = 1 / / Plus Box

Range.interior.colorindex: = 39; // Pack color is lavender

Range.font.name:= '书'; // font

Range.font.color: = CLBLUE; // Font Color

These often use these, these these are also applicable to a single cell.

Look for the first and lower cells in the Excel table:

Var U1, U2, U3, U4, U5: Variant;

U1: = Olecon.oleObject.Application.ActiveCell; // Gets the current grid;

U2: = u1.previous; // Non-special case is a one on the left of U1;

U3: = ui.next; // Non-special circumstances is a one in the right side of U2;

U4: = olecon.oleObject.Application.cells [u1.cells.row-1, u1.cells.column]; // Non-special cases are top

U5: = olecon.oleObject.Application.cells [u1.cells.row 1, u1.cells.column]; // Non-special cases are below

Delete and insert a row and a column:

Olecon.oleObject.Application.Rows [2] .delete;

Olecon.oleObject.Application.columns [2] .delete;

Olecon.oleObject.Application.Rows [2] .insert;

Olecon.oleObject.Application.columns [2] .insert;

Copy the specified area:

Olecon.oleObject.Application.range ['A1: C

3 '

] .copy;

Paste from the specified cell:

Olecon.oleObject.Application.range ['A

4 '

] .Pastespecial;

Commonly used these, the EXEL control under the Server panel in Delphi and the way to create an Excel.Application COM object is applicable.

Author: Fei Asia

time:

2004-9-14

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

New Post(0)