PhpExcel is encapsulated .? Php *** array generation Excel * @ authorzouhaozouhao619@gmail.com * use example * $ excelnewExcel (); $ dataarray (array (id1, name Destiny 1), array (id2, name Destiny 2); $ headera /**
* 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 ++ ){
$ Pisor = intval ($ number/26 );
$ Char = chr (64 + $ number % 26 );
$ Char = $ pisor = 0? $ Char: chr (64 + $ pisor). $ 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 );
Http://www.bkjia.com/PHPjc/477193.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477193.htmlTechArticle? Php/*** array to generate Excel * @ author zouhao zouhao619@gmail.com * use example * $ excel = new Excel (); $ data = array (id = 1, name = Destiny 1), array (id = 2, name = Destiny 2); $ header =...