Use Delphi 5 to transfer Excel 97 ----------------------------------------- ---------------------------------------- simply encapsulated in Delphi 5 Group Microsoft Office Automation Automation Servers. 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
Create a normal Application.
Put Excelapplication, Excelworkbook, and Excelworksheet in Form.
Connect Excel 97, the specific method is as follows: Open Excel97. TryExcelapplication1.connect; ExcePten; Excelapplication1.visible [0]: = true; Add a Workbook. Excelworkbook1.connectto (ExceLapplication1.Workbooks.Add (EmptyParam, 0));
Add a Worksheet. varTemp_Worksheet: _WorkSheet; beginTryTemp_Worksheet: = ExcelWorkbook1.WorkSheets.Add (EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0) as _WorkSheet; // (note) ExcelWorkSheet1.ConnectTo (Temp_WorkSheet); ExceptShowMessage ( 'Failure'); End; end; Close Excel.TryExcelApplication1.Quit; ExcelWorksheet1.Disconnect; ExcelWorkbook1.Disconnect; ExcelApplication1.Disconnect; ExceptEnd; ---- 4. Some of the operations of Excel: selecting certain current Workbook Worksheet.procedure TForm1.ComboBox1DropDown (Sender: TObject); VARI: Integer; BeginComboBox1.clear; for i: = 1 to Excelworkbook1.worksheets.count docomboBox1.items.add ((Excelworkbook1.worksheets.item [i] as _Worksheet) .name);
procedure TForm1.ComboBox1Change (Sender: TObject); beginExcelWorkSheet1.ConnectTo (ExcelWorkbook1.Worksheets.Item [ComboBox1.ItemIndex 1] as _WorkSheet); ExcelWorkSheet1.Activate; end;
Select a Workbook: procedure TForm1.ComboBox2DropDown (Sender: TObject); vari: Integer; beginComboBox2.Clear; if ExcelApplication1.Workbooks.Count> 0 thenFor i: = 1 to ExcelApplication1.Workbooks.Count doCombobox2.Items.Add (ExcelApplication1. Workbooks.Item [i] .Name); end; procedure TForm1.ComboBox2Change (Sender: TObject); beginExcelWorkSheet1.Disconnect; ExcelWorkBook1.ConnectTo (ExcelApplication1.Workbooks.Item [Combobox2.ItemIndex 1]); ExcelWorkBook1.Activate; ExcelWorksheet1. Connectto (Excelworkbook1.activeesheet as _Worksheet); Excelworksheet1.actiVate; End;
Assign a value and values for a cell. Procedure tform1.button5click (sender: TOBJECT); BeginexcelWorksheet1.cells.Item [Spinedit2.Value, Spiedit1.Value]: = Edit1.Text;
Procedure tform1.button6click (sender: Tobject); beginedit1.text: = Excelworksheet1.cells.Item [spinedit2.value, spinedit1.value];
Select a certain area Excelworksheet1.Range ['A1', 'C1']. Select;
Open an Excel file. if OpenDialog1.Execute thenBeginTryExcelWorkBook1.ConnectTo (ExcelApplication1.Workbooks.Open (OpenDialog1.FileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0)); ExcelWorkSheet1.ConnectTo ( Excelworkbook1.activeesheetas _worksheet;
---- 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.