Phpexcel Basic settings

Source: Internet
Author: User
Provides various official and user-released code examples, code reference, you are welcome to exchange and learn. Recently, we have been engaged in the export of excel. For some basic settings of table attributes, if you have any questions, I hope you can point them out to learn together.
// Introduce the class
Header ('content-type: text/html; charset = UTF-8 ');
Import ('org. Util. PHPExcel ');
Import ('org. Util. PHPExcel. Writer. excel5 ');
Import ('org. Util. PHPExcel. IOFactory. php ');
// Instantiate the object
$ ObjPHPExcel = new \ PHPExcel ();
// Set the workbook name
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('budget statement table ');
// Merge Cells
$ ObjPHPExcel-> getActiveSheet ()-> mergeCells ('a1: H1 ');
// Split Cells
// $ ObjPHPExcel-> getActiveSheet ()-> unmergeCells ('a1: H1 ');
// Set the cell font
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a2 ')-> getFont ()-> setName (' ') // font
-> SetSize (12) // font size
-> SetBold (true); // bold font

// Set the Row Height
$ ObjPHPExcel-> getActiveSheet ()-> getRowDimension ('1')-> setRowHeight (31.5 );
$ ObjPHPExcel-> getActiveSheet ()-> getRowDimension ('2')-> setRowHeight (31.5 );
$ ObjPHPExcel-> getActiveSheet ()-> getRowDimension ('3')-> setRowHeight (69.75 );
// Whether to wrap the line automatically when the length is insufficient.
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('B')-> getAlignment ()-> setWrapText (true );
// Set the orientation and size of the printed page (this is horizontal)
$ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setOrientation (\ PHPExcel_Worksheet_PageSetup: ORIENTATION_LANDSCAPE );
$ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setPaperSize (\ PHPExcel_Worksheet_PageSetup: PAPERSIZE_A4 );

// Freeze the title
// $ ObjPHPExcel-> getActiveSheet ()-> freezePane ('a1 ');
// $ ObjPHPExcel-> getActiveSheet ()-> freezePane ('a2 ');
$ ObjPHPExcel-> getActiveSheet ()-> freezePaneByColumnAndRow (8, 4); // (column, row)
// Set the horizontal center
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setHorizontal (\ PHPExcel_Style_Alignment: HORIZONTAL_CENTER );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b2')-> getAlignment ()-> setHorizontal (\ PHPExcel_Style_Alignment: HORIZONTAL_CENTER );
// Vertical center
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setVertical (\ PHPExcel_Style_Alignment: VERTICAL_CENTER );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b2')-> getAlignment ()-> setVertical (\ PHPExcel_Style_Alignment: VERTICAL_CENTER );
// Align left
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A')-> getAlignment ()-> setHorizontal (\ PHPExcel_Style_Alignment: HORIZONTAL_JUSTIFY );
// Right alignment
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A')-> getAlignment ()-> setHorizontal (\ PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
// Set the page margin to 0.5 cm (1 inch = 2.54 cm)
$ Margin = 1.78/2.54; // In phpexcel, it is calculated by inches.
$ Marginright = 1/2.54; // In phpexcel, it is calculated by inches.
// $ PageMargins-> setTop ($ margin); // top margin
// $ PageMargins-> setBottom ($ margin ); //
$ ObjPHPExcel-> getActiveSheet ()-> getPageMargins ()-> setLeft ($ margin); // left
$ ObjPHPExcel-> getActiveSheet ()-> getPageMargins ()-> setRight (marginright); // right
// $ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setFitToWidth ('1'); // The page width is automatically filled in.
// $ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setFitToHeight ('1'); // automatically fills in the page height
// Set the Title Index (click the title to jump to the corresponding workbook)
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('b4 ')-> getHyperlink ()-> setUrl ("sheet: //'" "'! A1 ");
// Set the border
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1: h8')-> getBorders ()-> getAllBorders ()-> setBorderStyle (\ PHPExcel_Style_Border: BORDER_THIN );

Phpexcel.rar (841.2 KB download: 17 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.