这里假定PHPExcel的放置目录为 library/Open
1.设置路径:
在public/index.php里定义include_path路径:
$path = 'library/Open';
$dir = dirname(dirname(dirname(__DIR__)))."/".$path;
set_include_path(get_include_path() . PATH_SEPARATOR . $dir);
2.调用方法:
require 'PHPExcel.php';
$objExcel = new \PHPExcel();
3.使用方法:
$objWriter = new \PHPExcel_Writer_Excel5($objExcel);
/*
$objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
$objWriter->setOffice2003Compatibility(true);
*/
$objProps = $objExcel->getProperties();
$objActSheet = $objExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle($app->name);
$objActSheet->getColumnDimension('A')->setWidth(6);
$objActSheet->getColumnDimension('B')->setWidth(50);
$objActSheet->getColumnDimension('C')->setWidth(30);
$objActSheet->getColumnDimension('D')->setWidth(10);
$objActSheet->getColumnDimension('E')->setWidth(30);
$num = 1;
$name = "A".$num;
$value = "序号";
$objActSheet->setCellValue($name, $value); //
$name = "B".$num;
$value = "udid";
$objActSheet->setCellValue($name, $value); //
$name = "C".$num;
$value = "Mac地址";
$objActSheet->setCellValue($name, $value); //
$name = "D".$num;
$value = "越狱";
$objActSheet->setCellValue($name, $value); //
$name = "E".$num;
$value = "日期";
$objActSheet->setCellValue($name, $value); //
//*************************************
//设置单元格内容
//
//由PHPExcel根据传入内容自动判断单元格内容类型
$num++;
foreach ($items as $key=>$item)
{
if(!isset($item->uuid)){
$item->uuid = "";
}
if(!isset($item->devmac)){
$item->devmac = "";
}
if(!isset($item->date)){
$item->date = "";
}
$name = "A".$num;
$value = $num-1;
$objActSheet->setCellValue($name, $value); //
$name = "B".$num;
$value = $item->uuid;
$objActSheet->setCellValue($name, $value); //
$name = "C".$num;
$value = $item->devmac;
$objActSheet->setCellValue($name, $value); //
$name = "D".$num;
$value = "";
if($item->devmac=="1"){
$value = "是";
}elseif($item->devmac=="0"){
$value = "否";
}
$objActSheet->setCellValue($name, $value); //
$name = "E".$num;
$value = $item->date;
$objActSheet->setCellValue($name, $value); //
$num++;
}
$outputFileName = $app->name.".xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outputFileName.'"');
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
exit;