Phpexcel Basic operations:
Defining Excel Entities
Defines a Phpexcel object and sets the display content within the Excel object
Excel starts
//prepares Excel for include file
//Error reporting
error_reporting (0);
Phpexcel
require_once dirname (__file__). ' phpexcel.php ';
Generates a new Excel object
$objPHPExcel = Phpexcel ();
Set the properties of an Excel document
$objPHPExcel->getproperties ()->setcreator ("Sam.c")
->setlastmodifiedby ("sam.c Test ")
->settitle (" Microsoft Office Excel Document ")
->setsubject (" test ")
-> SetDescription ("Test")->setkeywords ("Test")->setcategory ("test result
file");
Start Operation Excel Table
//Operation first worksheet
$objPHPExcel->setactivesheetindex (0);
Set workbook name
$objPHPExcel->getactivesheet ()->settitle (Iconv (' GBK ', ' utf-8 ', ' phpexcel test '));
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 output Excel format if ($m _exporttype== "Excel") {$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ')
;
Output the $filename header ("Pragma:public") directly from the browser;
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=". $filename);
Header ("Content-transfer-encoding:binary");
$objWriter->save ("Php://output");
//If you need to output 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");
}
To set the width of a column:
$objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (15);
To set the height of a row:
$objPHPExcel->getactivesheet ()->getrowdimension (' 6 ')->setrowheight (30);
Merge cells:
$objPHPExcel->getactivesheet ()->mergecells (' A1:p1 ');
Set A1 cells to bold, centered:
$styleArray 1 = array (
' font ' => array ('
bold ' => true,
' size ' =>12,
' 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);
$objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setbold (true);
To write content to a specific cell:
$objPHPExcel->getactivesheet ()->setcellvalue (' A1 ', ' Hello Baby ');
Set cell style (Horizontal/vertical Center):
$objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_center);
$objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center);
Set cell style (black font):
$objPHPExcel->getactivesheet ()->getstyle (' H5 ')->getfont ()->getcolor () ()->setargb (phpexcel_style_ Color::color_black); Black
Set cell formatting (background):
$objPHPExcel->getactivesheet ()->getstyle (' H5 ')->getfill ()->getstartcolor ("->setargb") ; Set the background to light pink
Format cell (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
$objDrawing = new phpexcel_worksheet_drawing ();
$objDrawing->setname (' Logo ');
$objDrawing->setdescription (' Logo ');
$objDrawing->setpath (' test.jpg ');
$objDrawing->setwidth ();
$objDrawing->setheight (123);
$objDrawing->setcoordinates (' J1 ');
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
To set a hyperlink in a cell:
$objPHPExcel->getactivesheet ()->setcellvalue (' H8 ', iconv (' GBK ', ' utf-8 ', ' Yan nan Tian '));
$objPHPExcel->getactivesheet ()->getcell (' H8 ')->gethyperlink ()->seturl (' http://www.jb51.net/');
Set cell borders
$styleThinBlackBorderOutline = Array ('
borders ' => Array ('
outline ' => array (
' style ') => _style_border::border_thin, //Set Border style
//' style ' => Phpexcel_style_border::border_thick, another style
' Color ' => Array (' ARGB ' => ' FF000000 '), //Set Border color),)
;
$objPHPExcel->getactivesheet ()->getstyle (' A4:e10 ')->applyfromarray ($styleThinBlackBorderOutline);
Add a new worksheet
$objExcel->createsheet ();
$objActSheet = $objExcel->getsheet ($s);
$objActSheet->settitle (' table '. $GSheet);