<?php /* * 类的功能 * 传入二位数组导出excel * 传入excel 导出二位数组 * @author mrwu */ require('PHPExcel.php'); require_once 'PHPExcel/Reader/Excel5.php'; include 'PHPExcel/IOFactory.php'; class plugin_phpexcel { private $export_excel_title;//导出excel标题 private $export_sheet_title;//导出sheet标题 private $letters; private $php_excel;//php_excel操作类 private $active_sheet; function __construct($export_excel_title='excel',$export_sheet_title='sheet1') { $this->letters=range('A','Z',1); $this->php_excel=new PHPExcel(); $this->php_excel->setActiveSheetIndex(0); $this->active_sheet=$this->php_excel->getActiveSheet(); $this->export_excel_title=$export_excel_title; $this->export_sheet_title=$export_sheet_title; } /* * $title='标题' array * $import_arr 插入excel的数组 要求二位数组 */ function export($title=array(),$import_arr) { //有设置excel第一行的标题 if($title) { $count=count($title); for($i=0;$i<=$count;$i++) { $this->active_sheet->setCellValue($this->letters[$i].'1',$title[$i]); } } //循环插入二维数组 $count=count($import_arr[0]); $row=1; foreach($import_arr as $value) { $row++; $j=0; foreach($value as $key=>$v) { $this->active_sheet->setCellValue($this->letters[$j].$row,$v); echo $value[$j]; $j++; } } $phpWriter=PHPExcel_IOFactory::createWriter($this->php_excel,'Excel5'); //设置一些标题等 $file=$this->export_excel_title; $this->active_sheet->setTitle($this->export_sheet_title); //设置header 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-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header('Content-Disposition:attachment;filename="excel.xls"'); header("Content-Transfer-Encoding:binary"); $phpWriter->save('php://output'); } } //测试 $php_excel=new plugin_phpexcel('excel','sheet1'); $php_excel->export(array('title','content'),array(array('title'=>'haha','content'=>'shit'),array('title'=>'hehe','content'=>'fuck')));