Use Delphi 5 to transfer Excel 97

zhaozj2021-02-08  232

Use Delphi 5 to transfer Excel 97

---- Simply encapsulate a group of Microsoft Office Automation objects in Delphi 5. It makes it easy for us to control the applications in Office (Word, Excel, PowerPoint, Outlook and Access, etc.) as a COM application server. In Delphi 5, you already have an example of Word and PowerPoint, because Excel calls are slightly different from the call of the two application servers, so I write a simple example of Excel 97 based on the two examples for reference.

---- Step together

1. Create a normal Application.

2. Put EXCELAPPLICATION, Excelworkbook, and Excelworksheet in Form.

3. Connect Excel 97, the specific method is as follows:

Open Excel97.

Try

Excelapplication1.connect;

Except

END;

Excelapplication1.visible [0]: = true;

Add a Workbook.

Excelworkbook1.connectto (ExceLapplication1.

Workbooks.add (EmptyParam, 0));

Add a Worksheet.

VAR

Temp_Worksheet: _Worksheet;

Begin

Try

Temp_Worksheet: = Excelworkbook1.

Worksheets.add (EmptyParam,

EMPTYPARAM, EMPTYPARAM, EMPTYPARAM, 0)

as _Worksheet; // (note)

Excelworksheet1.connectto (Temp_Worksheet);

Except

ShowMessage ('failure');

END;

END;

Close Excel.

Try

Excelapplication1.quit;

Excelworksheet1.disconnect;

Excelworkbook1.disconnect;

Excelapplication1.disconnect;

Except

END;

---- 4. Some operations to Excel:

Select a WorkSheet for Workbook.

Procedure TFORM1.COMBOBOX1DROPDOWN

Sender: TOBJECT;

VAR

i: integer;

Begin

ComboBox1.clear;

For i: = 1 to Excelworkbook1.

Worksheets.count do

ComboBox1.Items.Add

((Excelworkbook1.Worksheets.Item [i]

as _Worksheet) .name);

END;

Procedure TFORM1.COMBOBOX1CHANGE

Sender: TOBJECT;

Begin

Excelworksheet1.connectto

(Excelworkbook1.worksheets.Item

[ComboBoX1.ItemIndex 1] as _Worksheet;

Excelworksheet1.actiVate;

END;

Select a one Workbook:

Procedure TFORM1.COMBOBOX2DROPDOWN

Sender: TOBJECT;

VAR

i: integer;

Begin

ComboBox2.clear;

If Excelapplication1.Workbooks.count> 0 THEN

For i: = 1 to Excelapplication1.Workbooks.count DO

ComboBox2.Items.Add (Excelapplication1.workbooks.Item [i] .name);

END;

Procedure TFORM1.COMBOBOX2CHANGE (Sender: TOBJECT);

Begin

Excelworksheet1.disconnect;

Excelworkbook1.connectto (ExceLapplication1.Workbooks.

Item [ComboBox2.ItemIndex 1]);

Excelworkbook1.actiVate;

Excelworksheet1.connectto (Excelworkbook1.

Activesheet as _Worksheet);

Excelworksheet1.actiVate;

END;

Assign a value and values ​​for a cell.

Procedure TFORM1.BUTTON5CLICK (Sender: TOBJECT);

Begin

Excelworksheet1.cells.item [spinedit2.value,

Spinedit1.value]: = edit1.text;

END;

Procedure TFORM1.BUTTON6CLICK (Sender: TOBJECT);

Begin

Edit1.text: = Excelworksheet1.cells.Item [

Spinedit2.value, Spinedit1.Value];

END;

Select a certain area

Excelworksheet1.range ['A1', 'C1']. Select;

Open an Excel file.

IF openDialog1.execute the

Begin

Try

Excelworkbook1.connectto

(Excelapplication1.Workbooks.open

(OpenDialog1.FileName,

EmptyParam, EmptyParam, EMPTYPARAM,

EmptyParam, EMPTYPARAM,

EmptyParam, EmptyParam, EMPTYPARAM,

EmptyParam, EMPTYPARAM,

EMPTYPARAM, EMPTYPARAM, 0);

Excelworksheet1.connectto

(Excelworkbook1.activeesheet

as _Worksheet);

Except;

END;

END;

---- Description

---- This program runs in Win98 Delphi 5 Excel 97. This example can also be appropriately expanded, such as DDE, perform macro calls, save files, print files, and settings to Excel, see Microsoft Excel Visual Basic References in the Microsoft Excel object.

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

New Post(0)