PhpExcel in Encapsulation

Source: Internet
Author: User

<? Php
/**
* Array generation Excel
* @ Author zouhao zouhao619@gmail.com
* Example
* $ Excel = new Excel ();
$ Data = array (
Array ('id' => 1, 'name' => 'destiny 1 '),
Array ('id' => 2, 'name' => 'destiny 2 ')
);
$ Header = array ('id', 'Role name ');
$ Excel-> setFileName ('aaa ');
$ Excel-> setTitle ('workspace 1 ');
$ Excel-> create ($ data, $ header );
*/
Class Excel {
Private $ excelObj;
Private parameter filename='download.xls ';
/**
* Set the file name for download.
* @ Param string $ fileName
*/
Public function setFileName ($ fileName ){
$ This-> fileName=$fileName.'.xls ';
}
/**
* Set the title
* @ Param string $ title
*/
Public function setTitle ($ title ){
$ This-> excelObj-> getActiveSheet ()-> setTitle ($ title );
}
Public function _ construct (){
// Cancel the automatic registration mechanism of the original framework to avoid conflicts with the automatic excel loading mechanism.
Spl_autoload_unregister ('autoload ');
Require LIBRARY_PATH. '/PHPExcel. php ';
$ This-> excelObj = new PHPExcel ();
}
/**
* Returns the array of columns based on the total number.
*
* @ Param int $ count
* @ Return array
*/
Private function getCharByNumber ($ data ){
// Automatically subtract the header
$ Count = count ($ data ['0']);
$ Keys = array ();
For ($ number = 1; $ number <= $ count; $ number ++ ){
$ Divisor = intval ($ number/26 );
$ Char = chr (64 + $ number % 26 );
$ Char = $ divisor = 0? $ Char: chr (64 + $ divisor). $ char;
$ Keys [] = $ char;
}
Return $ keys;
}
/**
* Generate an Excel table
* @ Param array $ two-dimensional data array
* @ Param array $ replace the array to be replaced
*/
Public function create ($ data, $ header = array (), $ replace = null ){
Empty ($ data) and exit ('no data ');
$ Keys = $ this-> getCharByNumber ($ data );
$ This-> createHeader ($ header, $ keys );
$ J = 0;
Foreach ($ data as $ I =>$ vo ){
$ J = 0;
Foreach ($ vo as $ key => $ item ){
If (isset ($ replace [$ key]) {
$ This-> excelObj-> setActiveSheetIndex (0)-> setCellValue ($ keys [$ j]. ($ I + 2), $ replace [$ key] [$ item]);
} Else {
$ This-> excelObj-> setActiveSheetIndex (0)-> setCellValue ($ keys [$ j]. ($ I + 2), $ item );
}
+ + $ J;
}
}
// Outputs to the temporary buffer for download
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ('content-Disposition: inline; filename = "'. $ this-> fileName .'"');
Header ("Content-Transfer-Encoding: binary ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Pragma: no-cache ");
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ this-> excelObj, 'excel5 ');
$ ObjWriter-> save ('php: // output ');
}
/**
* Create a header
*
* @ Param array $ data
*/
Private function createHeader ($ header, $ keys ){
$ Header = array_combine ($ keys, $ header );
Foreach ($ header as $ key => $ vo ){
$ This-> excelObj-> setActiveSheetIndex (0)-> setCellValue ("{$ key} 1", $ vo );
}
}
}


Example:


$ Excel = new Excel ();
$ Data = array (
Array ('id' => 1, 'name' => 'destiny 1 '),
Array ('id' => 2, 'name' => 'destiny 2 ')
);
$ Header = array ('id', 'Role name ');
$ Excel-> setFileName ('aaa ');
$ Excel-> setTitle ('workspace 1 ');
$ Excel-> create ($ data, $ header); that is, the two-dimensional array derived from the database can be directly created,

In addition, some fields are of the type and saved as int type, which indicates a certain State. For details, refer to create third parameter.

Example:


$ Excel = new Excel ();
$ Data = array (
Array ('id' => 1, 'name' => 'destiny 1', 'type' => 1 ),
Array ('id' => 2, 'name' => 'destiny 2', 'type' => 2)
);
$ Replace ['type'] = array (1 => 'male', 2 => 'cute female ');
$ Header = array ('id', 'Role name ');
$ Excel-> setFileName ('aaa ');
$ Excel-> setTitle ('workspace 1 ');
$ Excel-> create ($ data, $ header, $ replace );

 

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.