Delphi control Excel2000

xiaoxiao2021-03-06  50

Source:

http://blog.9cbs.net/sitwo/archive/2004/09/16/106270.aspx

Category: Com & ActiveX

(1) Method using dynamic creation

First create an Excel object, use COMOBJ:

Var Excelapp: Variant;

Excelapp: = CreateoleObject ('Excel.Application');

1) Display the current window:

Excelapp.visible: = true;

2) Change the Excel title bar:

Excelapp.caption: = 'Application MICROSOFT EXCEL';

3) Add a new workbook:

Excelapp.workbooks.add;

4) Open an existing workbook:

Excelapp.workbooks.open ('c: /excel/demo.xls');

5) Set the second worksheet as an active worksheet:

Excelapp.worksheets [2] .activate;

or

Excelapp.Workssheets ['Sheet2'] .actiVate;

6) Give a cellular value:

Excelapp.cells [1,4] .value: = 'The first line of fourth column';

7) Set the width of the specified column (unit: characters), with the first list as an example:

Excelapp.activesheet.columns [1] .columnswidth: = 5;

8) Set the height of the specified line (unit: pound) (1 pound = 0.035 cm), with the second behavior:

Excelapp.activeesheet.rows [2 ].rtowheight: = 1 / 0.035; // 1 cm

9) Insert the pagper in the 8th line:

Excelapp.worksheets [1] .ROWS [8] .pagebreak: = 1;

10) Delete the paging before column 8:

Excelapp.activesheet.columns [4] .pagebreak: = 0;

11) Specify the width of the edge of the frame:

Excelapp.activeesheet.Range ['B3: D4'] .borders [2] .weight: = 3;

1-left 2- Right 3-top 4-bottom 5-oblique (/) 6-oblique (/)

12) Clear the first line of fourth column formula:

Excelapp.activeesheet.cells [1,4] .clearcontents;

13) Set the first line of font properties:

Excelapp.activesheet.Rows [1] .font.name: = 'Lishu';

Excelapp.activeesheet.Rows [1] .font.color: = CLBLUE

Excelapp.activesheet.Rows [1] .font.bold: = true;

Excelapp.activeesheet.Rows [1] .font.underline: = true;

14) Make page settings:

a. Header:

Excelapp.activesheet.pagesetup.centerhead.pagesetup.centerhead.pagesetup.Centerhead.PageSetup.Centerhead.PageSetup.Centerhead.PageSetup.Centerhead.PageSetup.Centerheader: = 'Report Demo';

b. Foot:

Excelapp.activeesheet.pagesetup.centerfooter: = 'P Typing';

c. Header to the top edge of 2cm:

Excelapp.activeesheet.pagesetup.headermargin: = 2 / 0.035;

d. Footer to the bottom side distance 3cm:

Excelapp.activesheet.pagesetup.headermargin: = 3 / 0.035;

e. The top distance 2cm:

Excelapp.activeesheet.pagesetup.topmargin: = 2 / 0.035; f. Bottom side distance 2cm:

Excelapp.activeesheet.pagesetup.bottommargin: = 2 / 0.035;

g. Left margins 2cm:

Excelapp.activeesheet.pagesetup.legtmargin: = 2 / 0.035;

h. Right margin 2cm:

Excelapp.activeesheet.pagesetup.rightmargin: = 2 / 0.035;

i. Page levels:

Excelapp.activesheet.pagesetup.centerhorizontally: = 2 / 0.035;

j. The page is vertical at:

Excelapp.activesheet.pagesetup.centerVertically: = 2 / 0.035;

k. Print unit cable:

Excelapp.activesheet.pagesetup.printgridlines: = true;

15) Copy operation:

a. Copy the entire worksheet:

Excelapp.activesheet.Used.range.copy;

b. Copy designated area:

Excelapp.activesheet.range ['a1: e2'] .copy;

c. Paste from the A1 position:

Excelapp.activesheet.range. ['A1'] .pastespecial;

d. Paste from the end of the file:

Excelapp.activeesheet.range.pastespecial;

16) Insert a row or a column:

a. eXcelapp.activesheet.rows [2] .insert;

b. eXcelapp.activesheet.columns [1] .insert;

17) Delete a row or one column:

a. eXcelapp.activesheet.rows [2] .delete;

b. eXcelapp.activesheet.columns [1] .delete;

18) Print preview worksheet:

Excelapp.activesheet.printpreview;

19) Print Output Worksheet:

Excelapp.activesheet.printout;

20) Worksheet Save:

if not excelapp.activeworkbook.saved dam

Excelapp.activesheet.printpreview;

21) Worksheet is saved as:

Excelapp.saveas ('c: /excel/demo1.xls');

22) Abandon the deposit:

Excelapp.ActiveWorkbook.saved: = True;

23) Close the workbook:

Excelapp.workbooks.close;

24) Exit Excel:

Excelapp.quit;

(2) Using Delphi Control Method

Put Excelapplication, Excelworkbook, and Excelworksheet in Form.

1) Open Excel

Excelapplication1.connect;

2) Display the current window:

Excelapplication1.visible [0]: = true;

3) Change the EXCEL title bar:

Excelapplication1.caption: = 'application call microsoft excel';

4) Add a new workbook:

Excelworkbook1.connectto (ExceLapplication1.Workbooks.Add (EmptyParam, 0));

5) Add a new worksheet:

VAR TEMP_WORKSHEET: _WORKSHEET

Begin

Temp_Worksheet: = Excelworkbook1.works Herbook (EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0) AS _WORKSHEET

Excelworksheet1.connectto (Temp_Worksheet);

END;

6) Open an existing workbook:

Excelapplication1.Workbooks.open (C: /A.XLS

EmptyParam, EmptyParam, EmptyParam, EmptyParam,

EmptyParam, EmptyParam, EmptyParam, EmptyParam,

EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0)

7) Set the second worksheet as an active worksheet:

Excelapplication1.Worksheets [2] .activate; or

Excelapplication1.Workssheets ['Sheet2'] .actiVate;

8) Give a unit value:

Excelapplication1.cells [1,4] .value: = 'The fourth column of the first line;

9) Set the width of the specified column (unit: characters), with the first list as an example:

Excelapplication1.activesheet.columns [1] .Columnswidth: = 5;

10) Set the height of the specified line (unit: pound) (1 pound = 0.035 cm), with the second behavior:

Excelapplication1.activeesheet.Rows [2 ].rtowheight: = 1 / 0.035; // 1 cm

11) Insert the pagper in the 8th line:

Excelapplication1.Worksheets [1] .ROWS [8] .pagebreak: = 1;

12) Delete the paging before column 8:

Excelapplication1.activeesheet.columns [4] .pagebreak: = 0;

13) Specify the width of the frame:

Excelapplication1.activeesheet.range ['b3: d4'] .borders [2] .weight: = 3;

1-left 2- Right 3-top 4-bottom 5-oblique (/) 6-oblique (/)

14) Clear the first line of fourth column formula:

Excelapplication1.activesheet.cells [1,4] .clearContents;

15) Set the first line of font properties:

Excelapplication1.activeesheet.Rows [1] .font.name: = '隶 书'

Excelapplication1.activesheet.Rows [1] .font.color: = CLBLUE

Excelapplication1.activeesheet.Rows [1] .font.bold: = true;

Excelapplication1.activesheet.Rows [1] .font.underline: = true;

16) Make page settings:

a. Header:

Excelapplication1.activeesheet.pagesetup.centerhead: = 'Report Demo';

b. Foot:

Excelapplication1.Activesheet.pagesetup.centerfooter: = 'P P Tropical;

c. Header to the top edge of 2cm:

Excelapplication1.activesheet.pagesetup.headermargin: = 2 / 0.035;

d. Footer to the bottom side distance 3cm:

Excelapplication1.Activesheet.pagesetup.headermarmargin: = 3 / 0.035; E. Top distance 2cm:

Excelapplication1.activeesheet.pagesetup.topmargin: = 2 / 0.035;

f. bottom margins 2cm:

Excelapplication1.Activesheet.pagesetup.bottommargin: = 2 / 0.035;

g. Left margins 2cm:

Excelapplication1.activesheet.pagesetup.Leftmargin: = 2 / 0.035;

h. Right margin 2cm:

Excelapplication1.activeesheet.pagesetup.rightmargin: = 2 / 0.035;

i. Page levels:

Excelapplication1.activesheet.pagesetup.centerhorizontally: = 2 / 0.035;

j. The page is vertical at:

Excelapplication1.activesheet.pagesetup.centerVertical: = 2 / 0.035;

k. Print unit cable:

Excelapplication1.Activesheet.pagesetup.printgridlines: = true;

17) Copy operation:

a. Copy the entire worksheet:

Excelapplication1.Activesheet.Used.range.copy;

b. Copy designated area:

Excelapplication1.activesheet.range ['A1: E2'] .copy;

c. Paste from the A1 position:

Excelapplication1.Activesheet.range. ['A1'] .pastespecial;

d. Paste from the end of the file:

Excelapplication1.activesheet.regs.pastespecial;

18) Insert a row or a column:

a. eXcelapplication1.activesheet.rows [2] .insert;

b. Excelapplication1.activesheet.columns [1] .insert;

19) Delete a row or a column:

a. excelapplication1.activeesheet.rows [2] .delete;

b. Excelapplication1.activeesheet.columns [1] .delete;

20) Print preview worksheet:

Excelapplication1.activesheet.printpreview;

21) Print Output Worksheet:

Excelapplication1.activesheet.printout;

22) Worksheet Save:

If not excelapplication1.activeworkbook.saved dam

Excelapplication1.activesheet.printpreview;

23) The worksheet is saved as:

Excelapplication1.saveas ('c: /excel/demo1.xls');

24) Abandon the deposit:

Excelapplication1.activeWorkbook.saved: = true;

25) Turn off the workbook:

Excelapplication1.workbooks.close;

26) Exit Excel:

Excelapplication1.quit;

Excelapplication1.disconnect;

(3) use Delphi to control the EXCLE two-dimensional map

Add Excelapplication, Excelworkbook and Excelworksheet, separately in Form

Var Asheet1, Achart, Range: Variant; 1) Select when the first work table

Asheet1: = Excelapplication1.Workbooks [1] .Worksheets [1];

2) Add a two-dimensional map

Achart: = asheet1.ChartObjects.Add (100, 100, 200, 200);

3) Select the form of two-dimensional map

Achart.Chart.ChartType: = 4;

4) Assign the 2D map

SERIES: = ACHART.CHART.SERIESCOLLECTION;

Range: = sheet1! r2c3: r3c9;

Series.Add (Range, True);

5) Plus the title of a two-dimensional figure

Achart.chart.hastitle: = true;

Achart.chart.charttitle.characters.text: = 'Excle two-dimensional map'

6) Change the title font size of the two-dimensional figure

Achart.chart.charttitle.font.size: = 6;

7) Add a marked note to the 2D figure

Achart.chart.axes (xlcategory, xlprimary) .haastitle: = true;

Achart.chart.axes (xlcategory, xlprimary) .axistry.characters.text: = 'subscript description';

8) Add the left mark for 2D chart

Achart.chart.axes (xlvalue, xlprimary) .haastitle: = true;

Achart.chart.axes (xlvalue, xlprimary) .axistitle.characters.text: = 'left label description'

9) Add to the 2D figure to right

Achart.chart.axes (xlvalue, xlsecondary) .haastitle: = true;

Achart.chart.axes (xlvalue, xlsecondary) .axistitle.characters.text: = 'Right-labeled ";

10) Change the size of the two-dimensional map

Achart.chart.Plotarea.LOTAREA.LOT: = 5;

Achart.Chart.Plotarea.width: = 223;

Achart.chart.Plotarea.height: = 108;

11) Add a description to the two-dimensional figure axle

Achart.Chart.seriesCollection [1] .name: = 'coordinate axis description'

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

New Post(0)