<? 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 );