Delphi Control Excel

xiaoxiao2021-03-06  64

1 Create an Excel file

To control Excel in Delphi, you must use OLE automation. OLE2 is now generally used to create an OLE object. When an OLE object is activated, the server program is only activated within the container program, which is the so-called "in-place activity".

When you create an Excel file, create an OLE object, then create a worksheet Worksheet in the object, such as the function CreateExcel:

Function CreateExcel: Variant;

VAR

Variant;

SHEET: VARIANT;

Begin

v: = creteoleObject ('excel.application'); // Create an OLE object

v.visible: = true;

v.Workbooks.add (-4167); // Add a worksheet

v.Workbooks [1] .Sheets [1] .name: = 'test'

Sheet: = v.workbooks [1] .sheets ['Test'];

Return V;

END;

2 data table control

The control of the Excel form mainly includes importing, modification; the merge of cells, the control of the border; the replication, paste, etc. of the table. When the report format is certain, the table is replicated, and the paste is particularly important so that one file template can be made first, and then the multi-page report can be output according to actual needs.

(1) Import (ImportData)

Procedure importdata;

VAR

I, J: Integer;

Variant;

Begin

v: = createExcel; // Create an Excel file test

For i: = 0 to MaxColumn DO

Begin

For j: = 0 to maxrow do

v.Workbooks [1] .sheets [1] .cells [i, j]: = i * j; // Import data

END;

END;

(2) Merging of cells, control of the border (LINESTYLECONTROL)

The merge of cells is carried out in the case where the merge range is selected. Border control can operate whether the border line is displayed. Other ways of control can be made in the process of following the process.

PROCEDURE LINESTYLECONTROL

VAR

v, Sheet, Range: Variant;

Begin

v: = creteexecl;

Sheet: = v.workbooks [1] .sheets [1];

Range: = Sheet.Range [Sheet.cells [1,1], Sheet.cells [39, 30]]; // Selected Form

Range.select;

Range.mege; // Merge unit

Range.Borders.LineStyle: = xlcontinuous; // The frame is visible

Range.font.size: = 9; // Change the size of the text in the table

END;

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

New Post(0)