Phpexcel in Package _php tutorial

Source: Internet
Author: User
/**
* Array Generation Excel
* @author Zouhao zouhao619@gmail.com
* Use 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 (' work area 1 ');
$excel->create ($data, $header);
*/
Class Excel {
Private $EXCELOBJ;
Private $fileName = ' Download.xls ';
/**
* Set file name when downloading
* @param string $fileName
*/
Public Function Setfilename ($fileName) {
$this->filename= $fileName. " XLS ';
}
/**
* Set Title
* @param string $title
*/
Public Function Settitle ($title) {
$this->excelobj->getactivesheet ()->settitle ($title);
}
Public Function __construct () {
Eliminate the automatic registration mechanism of the original frame and avoid conflicts with Excel auto-loading mechanism
Spl_autoload_unregister (' AutoLoad ');
Require Library_path. '/phpexcel/phpexcel.php ';
$this->excelobj = new Phpexcel ();
}
/**
* Returns an array of columns, based on total
*
* @param int $count
* @return Array
*/
Private Function Getcharbynumber ($data) {
Auto minus head
$count = count ($data [' 0 ']);
$keys =array ();
for ($number = 1; $number <= $count; $number + +) {
$divisor = Intval ($number/26);
$char = chr (+ $number% 26);
$char = $divisor = = 0? $char: Chr (+ $divisor). $char;
$keys [] = $char;
}
return $keys;
}
/**
* Generate Excel Table
* @param array $data two-dimensional arrays
* @param array $replace needs 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;
}
}
Output to temporary buffer provides 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 head
*
* @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);
}
}
}


Examples of Use:


$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 (' work area 1 ');
$excel->create ($data, $header); in other words, two-dimensional arrays taken from the database can be created directly,

In addition, some fields of type, which are saved as int, represent a certain state. You can refer to the create third parameter

Example:


$excel =new Excel ();
$data =array (
Array (' ID ' =>1, ' name ' = = ' Destiny 1 ', ' type ' =>1),
Array (' ID ' =>2, ' name ' = ' 2 ', ' type ' =>2)
);
$replace [' type ']=array (1=> ' macho ',2=> ' moe ');
$header =array (' ID ', ' role name ');
$excel->setfilename (' aaa ');
$excel->settitle (' work area 1 ');
$excel->create ($data, $header, $replace);

http://www.bkjia.com/PHPjc/477193.html www.bkjia.com true http://www.bkjia.com/PHPjc/477193.html techarticle The PHP/** * array generates Excel * @author Zouhao zouhao619@gmail.com * Use example * $excel =new Excel (); $data =array (Id=1,name= Day 1), Array (Id=2,name= Destiny 2)); $header =a ...

  • 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.