Include ("./class/class.php"); Basic header file containing class
- Include ("./class/phpexcel/phpexcel.php"); Generate basic class definitions for Excel (note the case of file names)
If you are outputting an Excel file directly, include this file
- Include ("./class/phpexcel/phpexcel/iofactory.php");
Creates a Phpexcel object that contains the contents and format of the output
- $m _objphpexcel = new Phpexcel ();
Template file, in order to achieve the separation of format and content, the specific content of the output file implemented in the template file
- Template file $m_objphpexcel object to be manipulated
- Include ("./include/excel.php");
Type of output file, Excel or PDF
- $m _exporttype = "Excel";
$m _stroutputexcelfilename = Date (' y-m-j_h_i_s '). ". XLS "; Output Excel file name
- $m _stroutputpdffilename = Date (' y-m-j_h_i_s '). ". PDF "; Output PDF file name
Phpexcel_iofactory, Output Excel
- Require_once dirname (__file__). ' /classes/phpexcel/iofactory.php ';
If you need to export Excel format
- if ($m _exporttype== "Excel") {
- $objWriter = Phpexcel_iofactory::createwriter ($m _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 ($m _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");
- }
- ?>
Copy Code2. template file content (additional common operations)
Global $m _objphpexcel; Defined by an external file
Set basic properties
- $m _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 LD report")
- ->setcategory ("Test result file");
Create multiple workbooks
- $sheet 1 = $m _objphpexcel->createsheet ();
- $sheet 2 = $m _objphpexcel->createsheet ();
Manipulate the corresponding workbook by manipulating the index
- Simply set the workbook index to be manipulated as the currently active workbook, as
- $m _objphpexcel->setactivesheetindex (0);
Set the first workbook as the active workbook
- $m _objphpexcel->setactivesheetindex (0);
Set the active workbook name
- If it is Chinese be sure to use the Iconv function to convert the encoding
- $m _objphpexcel->getactivesheet ()->settitle (Iconv (' GBK ', ' utf-8 ', ' Test Workbook ');
Set default font and size
- $m _objphpexcel->getdefaultstyle ()->getfont ()->setname (Iconv (' GBK ', ' utf-8 ', ' Arial '));
- $m _objphpexcel->getdefaultstyle ()->getfont ()->setsize (10);
Set the width of a column
- $m _objphpexcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (15);
Set the height of a row
- $m _objphpexcel->getactivesheet ()->getrowdimension (' 6 ')->setrowheight (30);
Merge cells
- $m _objphpexcel->getactivesheet ()->mergecells (' A1:p1 ');
Define a style, bold, centered
- $styleArray 1 = Array (
- ' Font ' = = Array (
- ' Bold ' = True,
- ' Color ' =>array (
- ' Argb ' = ' 00000000 ',
- ),
- ),
' Alignment ' = Array (
- ' Horizontal ' = Phpexcel_style_alignment::horizontal_center,
- ),
- );
Apply a style to a A1 cell
- $m _objphpexcel->getactivesheet ()->getstyle (' A1 ')->applyfromarray ($styleArray 1);
Set cell style (black font)
- $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getfont ()->getcolor ()->setargb (PHPExcel_Style _color::color_black); Black
formatting cells (background)
- $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getfill ()->getstartcolor ()->setargb (' 00ff99cc '); Set the background to light pink
Format cells (number format)
- $m _objphpexcel->getactivesheet ()->getstyle (' F1 ')->getnumberformat ()->setformatcode (' 0.000 ');
Write content to a specific cell
- $m _objphpexcel->getactivesheet ()->setcellvalue (' A1 ', ' Hello Baby ');
Set cell style (centered)
- $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getalignment ()->sethorizontal (PHPExcel_Style_ Alignment::horizontal_center);
Put the picture in the cell and put the data picture in the J1 cell then cell
- $objDrawing = new phpexcel_worksheet_drawing ();
- $objDrawing->setname (' Logo ');
- $objDrawing->setdescription (' Logo ');
- $objDrawing->setpath (". /logo.jpg "); Picture path, only relative path
- $objDrawing->setwidth (400); Picture width
- $objDrawing->setheight (123); Picture height
- $objDrawing->setcoordinates (' J1 ');//Cell
- $objDrawing->setworksheet ($m _objphpexcel->getactivesheet ());
Set A5 cell contents and add hyperlinks
- $m _objphpexcel->getactivesheet ()->setcellvalue (' A5 ', iconv (' GBK ', ' utf-8 ', ' Hyperlink jbxue.com '));
- $m _objphpexcel->getactivesheet ()->getcell (' A5 ')->gethyperlink ()->seturl (' http://bbs.it-home.org/') ;
- ?>
Copy Code3, in the server-side generation of static files compared to direct generation, the main difference between the two methods is the resulting format of the different, template files are identical, the bottom is a change on the basis of the above example, pay attention to the difference between the above example.
Basic header file containing class
- Include ("./class/class.php");
Generate basic class definitions for Excel (note the case of file names)
- Include ("./class/phpexcel/phpexcel.php");
- Contains the file written in Excel5 format, if you need to generate excel2007 files, including the corresponding writer can
- Include ("./class/phpexcel/phpexcel/writer/excel5.php");
- Contains files written in PDF format
- Include ("./class/phpexcel/phpexcel/writer/pdf.php");
Creates a Phpexcel object that contains the contents and format of the output
- $m _objphpexcel = new Phpexcel ();
Template file, in order to achieve the separation of format and content, the specific content of the output file implemented in the template file
- Template file $m_objphpexcel object to be manipulated
- Include ("./include/excel.php");
Type of output file, Excel or PDF
- $m _exporttype = "PDF";
$m _stroutputexcelfilename = Date (' y-m-j_h_i_s '). ". XLS "; Output Excel file name
- $m _stroutputpdffilename = Date (' y-m-j_h_i_s '). ". PDF "; Output PDF file name
Output file save path, this path must be writable
- $m _stroutputpath = "./output/";
If you need to export Excel format
- if ($m _exporttype== "Excel") {
- $objWriter = new Phpexcel_writer_excel5 ($m _objphpexcel);
- $objWriter->save ($m _stroutputpath. $m _stroutputexcelfilename);
- }
If you need to export PDF format
- if ($m _exporttype== "pdf") {
- $objWriter = new Phpexcel_writer_pdf ($m _objphpexcel);
- $objWriter->save ($m _stroutputpath. $m _stroutputpdffilename);
- }
- ?>
Copy Code |