Delphi Operation Excel Description

Source: Internet
Author: User

You can really quit Excel after you open Excel

Procedure Tform1.button1click (Sender:tobject);
Var
Excelapp1:variant;
Begin
Try
EXCELAPP1: = Createoleobject (' Excel.Application '); Open Excel
Except
Exit
End
ExcelApp1.WorkBooks.Open (Extractfilepath (application.exename) + ' \31a1.xls ');
EXCELAPP1.WORKSHEETS[1]. Activate;
EXCELAPP1.ACTIVESHEET.ROWS[1]. Copy;
ExcelApp1.ActiveSheet.Cells.Select;
ExcelApp1.ActiveSheet.UsedRange.Copy; Full selection of the current table
Memo1.pastefromclipboard;
Excelapp1.displayalerts:=false; Do not prompt to save
ExcelApp1.WorkBooks.Close;
Excelapp1.quit;
Excelapp1:= unassigned; Really quit
End

I didn't use the control operation, I usually write myself with Comobj. Look at the following things, I hope it will help you.

(i) Use dynamically created methods
to first create an Excel object, using Comobj:
var excelapp:variant;
Excelapp: = Createoleobject (' Excel.Application ');
1) Displays the current window:
excelapp.visible: = True;
2) Change the Excel title bar:
Excelapp.caption: = ' application calls Microsoft Excel ';
3) Add a new workbook:
ExcelApp.WorkBooks.Add;
4) Open a workbook that already exists:
ExcelApp.WorkBooks.Open (' C:\Excel\Demo.xls ');
5) Set 2nd worksheet as active sheet:
Excelapp.worksheets[2]. Activate;
or
excelapp.workssheets[' Sheet2 ']. Activate;
6) Assign a value to the cell:
excelapp.cells[1,4]. Value: = ' first row fourth column ';
7) Sets the width (in number of characters) of the specified column, in the first column as an example:
Excelapp.activesheet.columns[1]. Columnswidth: = 5;
8) Sets the height of the specified row (in points) (1 points = 0.035 centimeters), with the second behavior example:
Excelapp.activesheet.rows[2]. RowHeight: = 1/0.035; 1 cm

8, Text Horizontal center: excelid.worksheets[1]. ROWS[1]. HorizontalAlignment: = $FFFFEFF 4;
Text vertically centered: excelid.worksheets[1]. ROWS[1]. VerticalAlignment: = $FFFFEFF 4;
9. Insert one row or column: A. EXCELAPP.ACTIVESHEET.ROWS[2]. Insert;
B. excelapp.activesheet.columns[1]. Insert;
10. Delete one row or column: A. EXCELAPP.ACTIVESHEET.ROWS[2]. Delete;
B. excelapp.activesheet.columns[1]. Delete;

Sheet.PageSetup.Orientation: = 1; Page Setup direction (1: Portrait 2: Landscape)
11. Merge cell: Excelapp.worksheets[1].range[a1:f8 ']. Merge (ABC); NOTE: to declare variable abc:variant;
12, vertical lines display text: excelapp.worksheets[1]. cells.item[1,1]. orientation:=xlvertical;
13, Cell plus edge: excelapp.worksheets[1]. RANGE[A1:F8]. Borders.linestyle:= 1;

9) Insert a page break before line 8th:
EXCELAPP.WORKSHEETS[1]. ROWS[8]. PageBreak: = 1;
10) Delete the page break before the 8th column:
EXCELAPP.ACTIVESHEET.COLUMNS[4]. PageBreak: = 0;
11) Specify the width of the border line:
excelapp.activesheet.range[' B3:d4 '. BORDERS[2]. Weight: = 3;
1-Left 2-right 3-top 4-bottom 5-oblique (\) 6-oblique (/)
12) Clear the cell Formula for column Fourth of the first row:
excelapp.activesheet.cells[1,4]. clearcontents;
13) Set the first line font properties:
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Name: = ' official script ';
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Color: = Clblue;
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Bold: = True;
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.underline: = True;
14) make Page setup:
A. Header:
ExcelApp.ActiveSheet.PageSetup.CenterHeader: = ' report presentation ';
B. Footer:
ExcelApp.ActiveSheet.PageSetup.CenterFooter: = ' &p page ';
C. Header to top margin 2cm:
ExcelApp.ActiveSheet.PageSetup.HeaderMargin: = 2/0.035;
D. Footer End margin 3cm:
ExcelApp.ActiveSheet.PageSetup.HeaderMargin: = 3/0.035;
E. Top margin 2cm:
ExcelApp.ActiveSheet.PageSetup.TopMargin: = 2/0.035;
F. Bottom margin 2cm:
ExcelApp.ActiveSheet.PageSetup.BottomMargin: = 2/0.035;
G. Left margin 2cm:
ExcelApp.ActiveSheet.PageSetup.LeftMargin: = 2/0.035;
H. Right margin 2cm:
ExcelApp.ActiveSheet.PageSetup.RightMargin: = 2/0.035;
I. The page is centered horizontally:
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally: = true;
J. The page is centered vertically:
ExcelApp.ActiveSheet.PageSetup.CenterVertically: = false;;
K. Print the Cell network cable:
ExcelApp.ActiveSheet.PageSetup.PrintGridLines: = True;
15) Copy operation:
A. Copy the entire worksheet:
ExcelApp.ActiveSheet.UsedRange.Copy;
B. Copy the specified area:
excelapp.activesheet.range[' A1:e2 '. Copy;
C. Start pasting from A1 position:
ExcelApp.ActiveSheet.Range. [' A1 ']. PasteSpecial;
D. Start pasting from the tail of the file:
ExcelApp.ActiveSheet.Range.PasteSpecial;
16) Insert one row or column:
A. excelapp.activesheet.rows[2]. Insert;
B. excelapp.activesheet.columns[1]. Insert;
17) Delete one row or column:
A. excelapp.activesheet.rows[2]. Delete;
B. excelapp.activesheet.columns[1]. Delete;
18) Print Preview worksheet:
ExcelApp.ActiveSheet.PrintPreview;
19) Print out worksheet:
ExcelApp.ActiveSheet.PrintOut;
20) Worksheet Save:
If not ExcelApp.ActiveWorkBook.Saved then
ExcelApp.ActiveSheet.PrintPreview;
21) Save the worksheet as:
Excelapp.saveas (' C:\Excel\Demo1.xls ');
22) Discard the disk:
ExcelApp.ActiveWorkBook.Saved: = True;
23) Close Workbook:
ExcelApp.WorkBooks.Close;
24) Exit Excel:
Excelapp.quit;

II) using the Delphi control method
Put ExcelApplication, ExcelWorkbook, and excelworksheet in the form respectively.
1) Open Excel
Excelapplication1.connect;
2) Display the current window:
Excelapplication1.visible[0]:=true;
3) Change the Excel title bar:
Excelapplication1.caption: = ' application calls 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.
Worksheets.add (emptyparam,emptyparam,emptyparam,emptyparam,0) as _worksheet;
Excelworksheet1.connectto (Temp_worksheet);
End;
6) Open a workbook that already exists:
ExcelApplication1.Workbooks.Open (C:\a.xls
Emptyparam,emptyparam,emptyparam,emptyparam,
Emptyparam,emptyparam,emptyparam,emptyparam,
emptyparam,emptyparam,emptyparam,emptyparam,0)
7) Set the 2nd worksheet as the active sheet:
EXCELAPPLICATION1.WORKSHEETS[2]. Activate; Or
excelapplication1.workssheets[' Sheet2 '. Activate;
8) Assign a value to the cell:
excelapplication1.cells[1,4]. Value: = ' first row fourth column ';
9) Set the width of the specified column (in number of characters), in the first column as an example:
EXCELAPPLICATION1.ACTIVESHEET.COLUMNS[1]. Columnswidth: = 5;
10) Set the height of the specified line (in points) (1 lbs = 0.035 cm), with the second behavior example:
EXCELAPPLICATION1.ACTIVESHEET.ROWS[2]. RowHeight: = 1/0.035; 1 cm
11) Insert a page break before line 8th:
EXCELAPPLICATION1.WORKSHEETS[1]. ROWS[8]. PageBreak: = 1;
12) Delete the page break before the 8th column:
EXCELAPPLICATION1.ACTIVESHEET.COLUMNS[4]. PageBreak: = 0;
13) Specify the width of the border line:
excelapplication1.activesheet.range[' B3:d4 '. BORDERS[2]. Weight: = 3;
1-Left 2-right 3-top 4-bottom 5-oblique (\) 6-oblique (/)
14) Clear the cell Formula for column Fourth of the first row:
excelapplication1.activesheet.cells[1,4]. clearcontents;
15) Set the first line font properties:
EXCELAPPLICATION1.ACTIVESHEET.ROWS[1]. Font.Name: = ' official script ';
EXCELAPPLICATION1.ACTIVESHEET.ROWS[1]. Font.Color: = Clblue;
EXCELAPPLICATION1.ACTIVESHEET.ROWS[1]. Font.Bold: = True;
EXCELAPPLICATION1.ACTIVESHEET.ROWS[1]. Font.underline: = True;
16) make Page setup:
A. Header:
ExcelApplication1.ActiveSheet.PageSetup.CenterHeader: = ' report presentation ';
B. Footer:
ExcelApplication1.ActiveSheet.PageSetup.CenterFooter: = ' &p page ';
C. Header to top margin 2cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin: = 2/0.035;
D. Footer End margin 3cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin: = 3/0.035;
E. Top margin 2cm:
ExcelApplication1.ActiveSheet.PageSetup.TopMargin: = 2/0.035;
F. Bottom margin 2cm:
ExcelApplication1.ActiveSheet.PageSetup.BottomMargin: = 2/0.035;
G. Left margin 2cm:
ExcelApplication1.ActiveSheet.PageSetup.LeftMargin: = 2/0.035;
H. Right margin 2cm:
ExcelApplication1.ActiveSheet.PageSetup.RightMargin: = 2/0.035;
I. The page is centered horizontally:
ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally: = 2/0.035;
J. The page is centered vertically:
ExcelApplication1.ActiveSheet.PageSetup.CenterVertically: = 2/0.035;
K. Print the Cell network cable:
ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines: = True;
17) Copy operation:
A. Copy the entire worksheet:
ExcelApplication1.ActiveSheet.Used.Range.Copy;
B. Copy the specified area:
excelapplication1.activesheet.range[' A1:e2 '. Copy;
C. Start pasting from A1 position:
ExcelApplication1.ActiveSheet.Range. [' A1 ']. PasteSpecial;
D. Start pasting from the tail of the file:
ExcelApplication1.ActiveSheet.Range.PasteSpecial;
18) Insert one row or column:
A. excelapplication1.activesheet.rows[2]. Insert;
B. excelapplication1.activesheet.columns[1]. Insert;
19) Delete one row or column:
A. excelapplication1.activesheet.rows[2]. Delete;
B. excelapplication1.activesheet.columns[1]. Delete;
20) Print Preview worksheet:
ExcelApplication1.ActiveSheet.PrintPreview;
21) Print out worksheet:
ExcelApplication1.ActiveSheet.PrintOut;
22) Worksheet Save:
If not ExcelApplication1.ActiveWorkBook.Saved then
ExcelApplication1.ActiveSheet.PrintPreview;
23) Save the worksheet as:
Excelapplication1.saveas (' C:\Excel\Demo1.xls ');
24) Discard the disk:
ExcelApplication1.ActiveWorkBook.Saved: = True;
25) Close Workbook:
ExcelApplication1.WorkBooks.Close;
26) Exit Excel:
Excelapplication1.quit;
Excelapplication1.disconnect;

Delphi Operation Excel Description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.