Basic operation of PHPEXECL

Source: Internet
Author: User

The basic use method is divided into three parts:
First, the introduction of interfaces


Phpexcel_iofactory
Require_once dirname (__file__). ' /classes/phpexcel/iofactory.php ';


Ii. defining an Excel entity
Defines a Phpexcel object and sets the contents of the display within the Excel object

Excel starts
Prepare the include file for Excel
Error reporting
Error_reporting (E_all);

Phpexcel
Require_once dirname (__file__). ‘/.. /classes/phpexcel.php ';
Require_once dirname (__file__). ‘/.. /classes/phpexcel/richtext.php ';

To generate a new Excel object
$objPHPExcel = new Phpexcel ();

Set the properties of an Excel document
$objPHPExcel->getproperties ()->setcreator ("Sun Star Data Center")
->setlastmodifiedby ("Sun Star Data Center")
->settitle ("Microsoft Office Excel Document")
->setsubject ("Test Data report – from Sunstar data Center")
->setdescription ("LD Test Data Report, Generate by Sunstar data Center")
->setkeywords ("Sunstar Data Report")
->setcategory ("Test result file");

Start working with Excel tables
Manipulate the first worksheet
$objPHPExcel->setactivesheetindex (0);

Set Workbook name
$objPHPExcel->getactivesheet ()->settitle (Iconv (' GBK ', ' utf-8 ', ' Data report (this table is automatically generated by the Optical Data Center Program) ');

Set default font and size
$objPHPExcel->getdefaultstyle ()->getfont ()->setname (Iconv (' GBK ', ' utf-8 ', ' Arial '));
$objPHPExcel->getdefaultstyle ()->getfont ()->setsize (10);


Third, output file

If you need to export Excel format
if ($m _exporttype== "Excel") {
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');

Output $m_stroutputexcelfilename directly from the browser
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("content-type:application/vnd.ms-excel;");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header ("Content-disposition:attachment;filename=". $m _stroutputexcelfilename);
Header ("Content-transfer-encoding:binary");
$objWriter->save ("Php://output");
}

If you need to export PDF format
if ($m _exporttype== "pdf") {
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' PDF ');
$objWriter->setsheetindex (0);

Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/pdf");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header ("Content-disposition:attachment;filename=". $m _stroutputpdffilename);
Header ("Content-transfer-encoding:binary");
$objWriter->save ("Php://output");
}

Set the width of a column:
$objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (15);


Set the height of a row:
$objPHPExcel->getactivesheet ()->getrowdimension (' 6 ')->setrowheight (30);


Merge cells:
$objPHPExcel->getactivesheet ()->mergecells (' A1:p1 ');


Set A1 cell bold, centered:
$styleArray 1 = Array (
' Font ' = = Array (
' Bold ' = True,
' Color ' =>array (
' Argb ' = ' 00000000 ',
),
),
' Alignment ' = Array (
' Horizontal ' = Phpexcel_style_alignment::horizontal_center,
),
);
Set the A1 cell to bold, centered
$objPHPExcel->getactivesheet ()->getstyle (' A1 ')->applyfromarray ($styleArray 1);


To write content to a specific cell:
$objPHPExcel->getactivesheet ()->setcellvalue (' A1 ', ' Hello Baby ');


Set cell style (centered):
$objPHPExcel->getactivesheet ()->getstyle (' H5 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_center);


Set cell style (black font):
$objPHPExcel->getactivesheet ()->getstyle (' H5 ')->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_black); Black


formatting cells (background):
$objPHPExcel->getactivesheet ()->getstyle (' H5 ')->getfill ()->getstartcolor ()->setargb (' 00ff99cc ') ; Set the background to light pink


Format cells (number format):
$objPHPExcel->getactivesheet ()->getstyle (' F '. $iLineNumber)->getnumberformat ()->setformatcode (' 0.000 ');


To put a picture in a cell:
Place the data center picture in the J1 cell then cell
$objDrawing = new phpexcel_worksheet_drawing ();
$objDrawing->setname (' Logo ');
$objDrawing->setdescription (' Logo ');
$objDrawing->setpath ('.. /get/detail/images/logo_01.jpg ');
$objDrawing->setwidth (400);
$objDrawing->setheight (123);
$objDrawing->setcoordinates (' J1 ');
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());


To set a hyperlink in a cell:
$objPHPExcel->getactivesheet ()->setcellvalue (' J9 ', iconv (' GBK ', ' utf-8 ', ' Google '));
$objPHPExcel->getactivesheet ()->getcell (' J9 ')->gethyperlink ()->seturl (' http://www.g.cn/');

Basic operation of PHPEXECL

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.