EXCEL operation commands in Delphi

Source: Internet
Author: User

It is best to use Comobj to use Excel, which can achieve full control of excel.

However, this method does not provide code prompts in Delphi, but there are some ways to use the Server control!
Below is the use of comobj, the use of Server is not put! In addition, you need to release Variant once it is used up.
This is better (ExcelApp: = Unassigned ;)

Use Comobj
(1) Use the dynamic creation method
First, create an Excel Object and 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: = 'app calls Microsoft Excel ';
3) Add a new workbook:
ExcelApp. WorkBooks. Add;
4) open an existing workbook:
ExcelApp. WorkBooks. Open ('C:/Excel/Demo.xls ');
5) set 2nd worksheets as active worksheets:
ExcelApp. WorkSheets [2]. Activate;
Or
ExcelApp. WorksSheets ['sheet2']. Activate;
6) assign values to cells:
ExcelApp. Cells []. Value: = 'column 4 in the first row ';
7) set the width of the specified column (unit: number of characters). Take the first column as an example:
ExcelApp. ActiveSheet. Columns [1]. ColumnWidth: = 5;
8) set the height of the specified row (unit: lbs) (1 lbs = 0.035 cm). Take the second behavior as an example:
ExcelApp. ActiveSheet. Rows [2]. RowHeight: = 1/0. 035; // 1 cm
9) insert a paging character before Row 3:
ExcelApp. WorkSheets [1]. Rows [8]. PageBreak: = 1;
10) Delete the paging character before the 8th column:
ExcelApp. ActiveSheet. Columns [4]. PageBreak: = 0;
11) Specify the border line width:
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 in the fourth column of the first row:
ExcelApp. ActiveSheet. Cells [1, 4]. ClearContents;
13) set the font attribute of the first line:
ExcelApp. ActiveSheet. Rows [1]. Font. Name: = 'shanghai ';
ExcelApp. ActiveSheet. Rows [1]. Font. Color: = clBlue;
ExcelApp. ActiveSheet. Rows [1]. Font. Bold: = True;
Excelapp. activesheet. Rows [1]. Font. Underline: = true;
14) set the page:
A. header:
Excelapp. activesheet. pagesetup. centerheader: = 'report demo ';
B. footer:
Excelapp. activesheet. pagesetup. centerfooter: = 'page & P ';
C. The header to the top margin is 2 cm:
Excelapp. activesheet. pagesetup. headermargin: = 2/0. 035;
D. footer end margin 3 cm:
Excelapp. activesheet. pagesetup. headermargin: = 3/0. 035;
E. Top margin 2 cm:
Excelapp. activesheet. pagesetup. topmargin: = 2/0. 035;
F. The bottom margin is 2 cm:
Excelapp. activesheet. pagesetup. bottommargin: = 2/0. 035;
G. The left side is 2 cm:
Excelapp. activesheet. pagesetup. leftmargin: = 2/0. 035;
H. Right side: 2 cm:
Excelapp. activesheet. pagesetup. rightmargin: = 2/0. 035;
I. horizontal center of pages:
Excelapp. activesheet. pagesetup. centerhorizontally: = 2/0. 035;
J. Center the page vertically:
ExcelApp. ActiveSheet. PageSetup. CenterVertically: = 2/0. 035;
K. Print the cell network cable:
ExcelApp. ActiveSheet. PageSetup. PrintGridLines: = True;
15) copy operation:
A. Copy the entire Worksheet:
ExcelApp. ActiveSheet. Used. Range. Copy;
B. Copy the Specified Region:
ExcelApp. ActiveSheet. Range ['a1: e2']. Copy;
C. paste the file at A1:
ExcelApp. ActiveSheet. Range. ['a1']. PasteSpecial;
D. paste it from the end of the file:
ExcelApp. ActiveSheet. Range. PasteSpecial;
16) insert a row or column:
A. ExcelApp. ActiveSheet. Rows [2]. Insert;
B. ExcelApp. ActiveSheet. Columns [1]. Insert;
17) delete a row or column:
A. ExcelApp. ActiveSheet. Rows [2]. Delete;
B. ExcelApp. ActiveSheet. Columns [1]. Delete;
18) print the preview Worksheet:
ExcelApp. ActiveSheet. PrintPreview;
19) print the output Worksheet:
ExcelApp. ActiveSheet. PrintOut;
20) Save the worksheet:
If not ExcelApp. ActiveWorkBook. Saved then
ExcelApp. ActiveSheet. PrintPreview;
21) Save the worksheet as follows:
ExcelApp. SaveAs ('C:/Excel/Demo1.xls ');
22) discard the storage:
ExcelApp. ActiveWorkBook. Saved: = True;
23) Close the workbook:
ExcelApp. WorkBooks. Close;
24) Exit Excel:
ExcelApp. Quit;

 

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.